Regular Express to remove all text between to ,'s

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stitch Jones

    Regular Express to remove all text between to ,'s

    I'm trying to get part of my perl script using a regex to get rid of
    an entire field between two ,'s I'm doing this to get a .csv formated
    alot nicer. here is an example of my issue
    user ,jibberjibber 1234456hithere( hkl)- special char [0]jibber, 4567
    I need the keep the user part and the end 4567 part but all the stuff
    between the two ,'s has gotta go. Someone Please help.
    Thank you.
  • Simon Proctor

    #2
    Re: Regular Express to remove all text between to ,'s

    Stitch Jones wrote:
    [color=blue]
    > I'm trying to get part of my perl script using a regex to get rid of
    > an entire field between two ,'s I'm doing this to get a .csv formated
    > alot nicer. here is an example of my issue
    > user ,jibberjibber 1234456hithere( hkl)- special char [0]jibber, 4567
    > I need the keep the user part and the end 4567 part but all the stuff
    > between the two ,'s has gotta go. Someone Please help.
    > Thank you.[/color]

    s!^([^,]*),[^,]*?,(.*)$!$1$2!;

    This assumes that the line given is formatted as you defined and you only
    want to remove the first lot of comma defined text.


    --
    Simon Proctor

    Sometimes you just have to wonder...

    Comment

    • Shawn Zabel

      #3
      Re: Regular Express to remove all text between to ,'s

      "Stitch Jones" <stchjnes@nycap .rr.com> wrote in message
      news:fsugi0tmbb kqh9v7lr76fnn5l p52uu1idg@4ax.c om...[color=blue]
      > I'm trying to get part of my perl script using a regex to get rid of
      > an entire field between two ,'s I'm doing this to get a .csv formated
      > alot nicer. here is an example of my issue
      > user ,jibberjibber 1234456hithere( hkl)- special char [0]jibber, 4567
      > I need the keep the user part and the end 4567 part but all the stuff
      > between the two ,'s has gotta go. Someone Please help.
      > Thank you.[/color]


      The following regex will substitute everything between the first and last
      comma (including the commas on each end) with a comma. The result based on
      your example will be "user , 4567" without the quotes.

      s/,.*,/,/


      - Shawn



      Comment

      • Joe Smith

        #4
        Re: Regular Express to remove all text between to ,'s

        Stitch Jones wrote:
        [color=blue]
        > I'm trying to get part of my perl script using a regex to get rid of
        > an entire field between two ,'s[/color]

        What have you tried? Post a snippet of your code to the proper
        newsgroup (comp.lang.perl .misc) instead of here (comp.lang.perl ).
        [Do you know the difference between s/,.*,/,/ and s/,.*?,/,/ ?]
        -Joe

        Comment

        • Stitch Jones

          #5
          Re: Regular Express to remove all text between to ,'s

          On Sun, 22 Aug 2004 12:20:12 -0400, "Shawn Zabel" <zabelsr@alltel .net>
          wrote:
          [color=blue]
          >"Stitch Jones" <stchjnes@nycap .rr.com> wrote in message
          >news:fsugi0tmb bkqh9v7lr76fnn5 lp52uu1idg@4ax. com...[color=green]
          >> I'm trying to get part of my perl script using a regex to get rid of
          >> an entire field between two ,'s I'm doing this to get a .csv formated
          >> alot nicer. here is an example of my issue
          >> user ,jibberjibber 1234456hithere( hkl)- special char [0]jibber, 4567
          >> I need the keep the user part and the end 4567 part but all the stuff
          >> between the two ,'s has gotta go. Someone Please help.
          >> Thank you.[/color]
          >
          >
          >The following regex will substitute everything between the first and last
          >comma (including the commas on each end) with a comma. The result based on
          >your example will be "user , 4567" without the quotes.
          >
          >s/,.*,/,/
          >
          >
          >- Shawn
          >
          >[/color]

          Shawn, Thank you. That was what I needed. I was overthinking it. I
          thought I needed to come up with a fancy string to parse through it.
          I'll have to start brushing up on my Reg-Ex 101 stuff.
          Thank You.

          Comment

          Working...