Regular expression problem, help please!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    Regular expression problem, help please!

    Hi

    I need to write one regex to read all the fields from the following lines /
    file format
    line 1 - some_alphanumer ic,some_alphanu meric,"somethin g,
    something",numb ers_hyphenatedO Rnot
    line 2 - some_alphanumer ic,some_alphanu meric,something
    something,numbe rs_hyphenatedOR not

    At first I thought this one will do
    "[^"\r\n]*",|[A-Za-z0-9 ]*,|[0-9]*\-[0-9]*

    but I am getting the delimiters such as the trailing comma and the
    double-quote along with the fields !!

    Can this be modified to get the fields only, or at least to get rid of the
    trailing comma?

    TIA


    --



  • James Curran

    #2
    Re: Regular expression problem, help please!

    > line 1 - some_alphanumer ic,

    [A-Za-z0-9 ]*,

    some_alphanumer ic,
    [A-Za-z0-9 ]*,

    "something, something",
    "[~"]*",

    numbers_hyphena tedORnot
    [0-9-]*

    All together: [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,"[~"]*",[0-9-]*
    [color=blue]
    > line 2 - some_alphanumer ic,some_alphanu meric,something
    > something,numbe rs_hyphenatedOR not[/color]

    [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[0-9-]*

    Combine it all into one big one:
    [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,("[^"]*")|([A-Za-z0-9 ]*),[0-9-]*

    Any character in "A-Za-z0-9 "
    * (zero or more times)
    ,
    Any character in "A-Za-z0-9 "
    * (zero or more times)
    ,
    Capture
    "
    Any character not in """
    * (zero or more times)
    "
    End Capture
    or
    Capture
    Any character in "A-Za-z0-9 "
    * (zero or more times)
    End Capture
    ,
    Any character in "0-9-"
    * (zero or more times)

    (Interpretation via Regular Expression Workbench)


    --
    --
    Truth,
    James Curran
    [erstwhile VC++ MVP]

    Home: www.noveltheory.com Work: www.njtheater.com
    Blog: www.honestillusion.com Day Job: www.partsearch.com

    <dl> wrote in message news:eTal$XfZFH A.2664@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hi
    >
    > I need to write one regex to read all the fields from the following lines[/color]
    /[color=blue]
    > file format
    > line 1 - some_alphanumer ic,some_alphanu meric,"somethin g,
    > something",numb ers_hyphenatedO Rnot
    > line 2 - some_alphanumer ic,some_alphanu meric,something
    > something,numbe rs_hyphenatedOR not
    >
    > At first I thought this one will do
    > "[^"\r\n]*",|[A-Za-z0-9 ]*,|[0-9]*\-[0-9]*
    >
    > but I am getting the delimiters such as the trailing comma and the
    > double-quote along with the fields !!
    >
    > Can this be modified to get the fields only, or at least to get rid of the
    > trailing comma?
    >
    > TIA
    >
    >
    > --
    >
    >
    >[/color]


    Comment

    • Guest's Avatar

      #3
      Re: Regular expression problem, help please!

      Hi Curran

      Thanks.
      With some minor changes, I get a much cleaner one (compare to the one I
      originally had)
      [A-Za-z0-9 -]*,|("[^"]*"),|[0-9 ()-]*
      but I have one question
      1. there is still trialing comma, I guess we can't get rid of it, and just
      have to trim it off, right?

      TIA

      "James Curran" <jamescurran@mv ps.org> wrote in message
      news:eyf5h%23fZ FHA.2496@TK2MSF TNGP14.phx.gbl. ..[color=blue][color=green]
      > > line 1 - some_alphanumer ic,[/color]
      >
      > [A-Za-z0-9 ]*,
      >
      > some_alphanumer ic,
      > [A-Za-z0-9 ]*,
      >
      > "something, something",
      > "[~"]*",
      >
      > numbers_hyphena tedORnot
      > [0-9-]*
      >
      > All together: [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,"[~"]*",[0-9-]*
      >[color=green]
      > > line 2 - some_alphanumer ic,some_alphanu meric,something
      > > something,numbe rs_hyphenatedOR not[/color]
      >
      > [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[0-9-]*
      >
      > Combine it all into one big one:
      > [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,("[^"]*")|([A-Za-z0-9 ]*),[0-9-]*
      >
      > Any character in "A-Za-z0-9 "
      > * (zero or more times)
      > ,
      > Any character in "A-Za-z0-9 "
      > * (zero or more times)
      > ,
      > Capture
      > "
      > Any character not in """
      > * (zero or more times)
      > "
      > End Capture
      > or
      > Capture
      > Any character in "A-Za-z0-9 "
      > * (zero or more times)
      > End Capture
      > ,
      > Any character in "0-9-"
      > * (zero or more times)
      >
      > (Interpretation via Regular Expression Workbench)
      >
      >
      > --
      > --
      > Truth,
      > James Curran
      > [erstwhile VC++ MVP]
      >
      > Home: www.noveltheory.com Work: www.njtheater.com
      > Blog: www.honestillusion.com Day Job: www.partsearch.com
      >
      > <dl> wrote in message news:eTal$XfZFH A.2664@TK2MSFTN GP15.phx.gbl...[color=green]
      > > Hi
      > >
      > > I need to write one regex to read all the fields from the following[/color][/color]
      lines[color=blue]
      > /[color=green]
      > > file format
      > > line 1 - some_alphanumer ic,some_alphanu meric,"somethin g,
      > > something",numb ers_hyphenatedO Rnot
      > > line 2 - some_alphanumer ic,some_alphanu meric,something
      > > something,numbe rs_hyphenatedOR not
      > >
      > > At first I thought this one will do
      > > "[^"\r\n]*",|[A-Za-z0-9 ]*,|[0-9]*\-[0-9]*
      > >
      > > but I am getting the delimiters such as the trailing comma and the
      > > double-quote along with the fields !!
      > >
      > > Can this be modified to get the fields only, or at least to get rid of[/color][/color]
      the[color=blue][color=green]
      > > trailing comma?
      > >
      > > TIA
      > >
      > >
      > > --
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • cody

        #4
        Re: Regular expression problem, help please!

        instead of the last * use a + which means "count bigger than one" so that
        the comma should disappear.


        <dl> schrieb im Newsbeitrag news:uLoNAlkZFH A.2900@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Hi Curran
        >
        > Thanks.
        > With some minor changes, I get a much cleaner one (compare to the one I
        > originally had)
        > [A-Za-z0-9 -]*,|("[^"]*"),|[0-9 ()-]*
        > but I have one question
        > 1. there is still trialing comma, I guess we can't get rid of it, and just
        > have to trim it off, right?
        >
        > TIA
        >
        > "James Curran" <jamescurran@mv ps.org> wrote in message
        > news:eyf5h%23fZ FHA.2496@TK2MSF TNGP14.phx.gbl. ..[color=green][color=darkred]
        > > > line 1 - some_alphanumer ic,[/color]
        > >
        > > [A-Za-z0-9 ]*,
        > >
        > > some_alphanumer ic,
        > > [A-Za-z0-9 ]*,
        > >
        > > "something, something",
        > > "[~"]*",
        > >
        > > numbers_hyphena tedORnot
        > > [0-9-]*
        > >
        > > All together: [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,"[~"]*",[0-9-]*
        > >[color=darkred]
        > > > line 2 - some_alphanumer ic,some_alphanu meric,something
        > > > something,numbe rs_hyphenatedOR not[/color]
        > >
        > > [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[A-Za-z0-9 ]*,[0-9-]*
        > >
        > > Combine it all into one big one:
        > > [A-Za-z0-9 ]*,[A-Za-z0-9 ]*,("[^"]*")|([A-Za-z0-9 ]*),[0-9-]*
        > >
        > > Any character in "A-Za-z0-9 "
        > > * (zero or more times)
        > > ,
        > > Any character in "A-Za-z0-9 "
        > > * (zero or more times)
        > > ,
        > > Capture
        > > "
        > > Any character not in """
        > > * (zero or more times)
        > > "
        > > End Capture
        > > or
        > > Capture
        > > Any character in "A-Za-z0-9 "
        > > * (zero or more times)
        > > End Capture
        > > ,
        > > Any character in "0-9-"
        > > * (zero or more times)
        > >
        > > (Interpretation via Regular Expression Workbench)
        > >
        > >
        > > --
        > > --
        > > Truth,
        > > James Curran
        > > [erstwhile VC++ MVP]
        > >
        > > Home: www.noveltheory.com Work: www.njtheater.com
        > > Blog: www.honestillusion.com Day Job: www.partsearch.com
        > >
        > > <dl> wrote in message news:eTal$XfZFH A.2664@TK2MSFTN GP15.phx.gbl...[color=darkred]
        > > > Hi
        > > >
        > > > I need to write one regex to read all the fields from the following[/color][/color]
        > lines[color=green]
        > > /[color=darkred]
        > > > file format
        > > > line 1 - some_alphanumer ic,some_alphanu meric,"somethin g,
        > > > something",numb ers_hyphenatedO Rnot
        > > > line 2 - some_alphanumer ic,some_alphanu meric,something
        > > > something,numbe rs_hyphenatedOR not
        > > >
        > > > At first I thought this one will do
        > > > "[^"\r\n]*",|[A-Za-z0-9 ]*,|[0-9]*\-[0-9]*
        > > >
        > > > but I am getting the delimiters such as the trailing comma and the
        > > > double-quote along with the fields !!
        > > >
        > > > Can this be modified to get the fields only, or at least to get rid of[/color][/color]
        > the[color=green][color=darkred]
        > > > trailing comma?
        > > >
        > > > TIA
        > > >
        > > >
        > > > --
        > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...