Help with a Reg Ex

Discussion in 'Perl Misc' started by amerar@iwc.net, Jan 9, 2008.

  1. Guest

    I have the following expression in my Perl script:

    if (/^"([^\,].+)"\,"$/)

    Although I know that it is checking to see if the string starts with
    some characters, it is not working for my input file.

    Can someone explain to me in english what it is doing???

    Thanks in advance.
     
    , Jan 9, 2008
    #1
    1. Advertising

  2. On Wed, 09 Jan 2008 11:00:18 -0800, wrote:

    > I have the following expression in my Perl script:
    >
    > if (/^"([^\,].+)"\,"$/)
    >
    > Although I know that it is checking to see if the string starts with
    > some characters, it is not working for my input file.
    >
    > Can someone explain to me in english what it is doing???


    /
    ^ # beginning of string
    " # match a quote
    ( # start capturing
    [^\,] # one of any character, except comma
    .+ # one of more of any character, except newline
    ) # stop capturing
    " # another quote
    \, # a comma
    " # yet another quote
    $ # end of string
    /x

    In other words, any string that starts with a quote; then two or more
    characters, but not starting with a comma; a quote; a comma and a quote.

    What do you want to achieve?

    M4
     
    Martijn Lievaart, Jan 9, 2008
    #2
    1. Advertising

  3. Guest

    On Jan 9, 2:39 pm, Martijn Lievaart <> wrote:
    > On Wed, 09 Jan 2008 11:00:18 -0800, wrote:
    > > I have the following expression in my Perl script:

    >
    > > if (/^"([^\,].+)"\,"$/)

    >
    > > Although I know that it is checking to see if the string starts with
    > > some characters, it is not working for my input file.

    >
    > > Can someone explain to me in english what it is doing???

    >
    > /
    > ^ # beginning of string
    > " # match a quote
    > ( # start capturing
    > [^\,] # one of any character, except comma
    > .+ # one of more of any character, except newline
    > ) # stop capturing
    > " # another quote
    > \, # a comma
    > " # yet another quote
    > $ # end of string
    > /x
    >
    > In other words, any string that starts with a quote; then two or more
    > characters, but not starting with a comma; a quote; a comma and a quote.
    >
    > What do you want to achieve?
    >
    > M4


    Basically I have an input file, and the result of that IF statement
    decides if I process the line read of not.......
     
    , Jan 9, 2008
    #3
  4. Guest

    On Jan 9, 2:39 pm, Martijn Lievaart <> wrote:
    > On Wed, 09 Jan 2008 11:00:18 -0800, wrote:
    > > I have the following expression in my Perl script:

    >
    > > if (/^"([^\,].+)"\,"$/)

    >
    > > Although I know that it is checking to see if the string starts with
    > > some characters, it is not working for my input file.

    >
    > > Can someone explain to me in english what it is doing???

    >
    > /
    > ^ # beginning of string
    > " # match a quote
    > ( # start capturing
    > [^\,] # one of any character, except comma
    > .+ # one of more of any character, except newline
    > ) # stop capturing
    > " # another quote
    > \, # a comma
    > " # yet another quote
    > $ # end of string
    > /x
    >
    > In other words, any string that starts with a quote; then two or more
    > characters, but not starting with a comma; a quote; a comma and a quote.
    >
    > What do you want to achieve?
    >
    > M4


    I might add, the input file looks like this:

    "#ABC","
    [
    ","ABC Dispensing designs, manufactures and services dispensing
    systems that utilize standard micro-processing technology and
    proprietary operating software.","
    ]
    "
    "#ABE","
    [
    ","Aber Diamond Corporation is a specialist diamond company focusing
    on the mining and retail segments of the diamond industry. The Company
    supplies rough diamonds to the global market through its forty percent
    ownership in the Diavik Diamond Mine and owns one of the world's
    premier retailers of diamond jewelry, Harry Winston.","
    ]
    "

    And I guess it is deciding if the line needs to be processed.......
     
    , Jan 9, 2008
    #4
  5. "" <> wrote:
    >
    >I have the following expression in my Perl script:
    >
    >if (/^"([^\,].+)"\,"$/)
    >
    >Although I know that it is checking to see if the string starts with
    >some characters, it is not working for my input file.
    >
    >Can someone explain to me in english what it is doing???


    Match a string that
    ^ starts with
    " a double quote
    ( and then a group that consists of
    [^\,] one of the three characters caret, backslash, comma
    ..+ followed by at least one more character
    )
    "\," and has the character sequence double quote, comma, doublequote
    $ at the end of the string.

    I hope I got that right.

    jue
     
    Jürgen Exner, Jan 9, 2008
    #5
  6. Jürgen Exner <> wrote:
    >"" <> wrote:
    >>
    >>I have the following expression in my Perl script:
    >>
    >>if (/^"([^\,].+)"\,"$/)
    >>
    >>Although I know that it is checking to see if the string starts with
    >>some characters, it is not working for my input file.
    >>
    >>Can someone explain to me in english what it is doing???

    >
    >Match a string that
    >^ starts with
    >" a double quote
    >( and then a group that consists of
    >[^\,] one of the three characters caret, backslash, comma


    Ooops, correction. Make that
    [^\,] any single character, that is not backslash or comma

    >.+ followed by at least one more character
    >)
    >"\," and has the character sequence double quote, comma, doublequote
    >$ at the end of the string.
    >
    >I hope I got that right.
    >
    >jue
     
    Jürgen Exner, Jan 9, 2008
    #6
  7. Jürgen Exner wrote:
    > Jürgen Exner <> wrote:
    >> "" <> wrote:
    >>> I have the following expression in my Perl script:
    >>>
    >>> if (/^"([^\,].+)"\,"$/)
    >>>
    >>> Although I know that it is checking to see if the string starts with
    >>> some characters, it is not working for my input file.
    >>>
    >>> Can someone explain to me in english what it is doing???

    >> Match a string that
    >> ^ starts with
    >> " a double quote
    >> ( and then a group that consists of
    >> [^\,] one of the three characters caret, backslash, comma

    >
    > Ooops, correction. Make that
    > [^\,] any single character, that is not backslash or comma


    You were right the first time. A backslash before a normal character in
    a double quoted string is superfluous.

    $ perl -le'print qr/[^\,]/'
    (?-xism:[^,])



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jan 9, 2008
    #7
  8. Jürgen Exner wrote:
    > Jürgen Exner <> wrote:
    >> "" <> wrote:
    >>> I have the following expression in my Perl script:
    >>>
    >>> if (/^"([^\,].+)"\,"$/)
    >>>
    >>> Although I know that it is checking to see if the string starts with
    >>> some characters, it is not working for my input file.
    >>>
    >>> Can someone explain to me in english what it is doing???

    >> Match a string that
    >> ^ starts with
    >> " a double quote
    >> ( and then a group that consists of
    >> [^\,] one of the three characters caret, backslash, comma

    >
    > Ooops, correction. Make that
    > [^\,] any single character, that is not backslash or comma


    A backslash before a normal character in a double quoted string is
    superfluous.

    $ perl -le'print qr/[^\,]/'
    (?-xism:[^,])



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jan 9, 2008
    #8
  9. On Wed, 09 Jan 2008 20:40:49 +0000, Jürgen Exner wrote:

    > Jürgen Exner <> wrote:
    >>"" <> wrote:
    >>>
    >>>I have the following expression in my Perl script:
    >>>
    >>>if (/^"([^\,].+)"\,"$/)
    >>>
    >>>Although I know that it is checking to see if the string starts with
    >>>some characters, it is not working for my input file.
    >>>
    >>>Can someone explain to me in english what it is doing???

    >>
    >>Match a string that
    >>^ starts with
    >>" a double quote
    >>( and then a group that consists of
    >>[^\,] one of the three characters caret, backslash,

    comma
    >
    > Ooops, correction. Make that
    > [^\,] any single character, that is not backslash or

    comma

    No, is not comma:

    [martijn@dexter ~]$ perl -e 'print "yes\n" if "," =~ /[^\,]/'
    [martijn@dexter ~]$ perl -e 'print "yes\n" if "\\" =~ /[^\,]/'
    yes
    [martijn@dexter ~]$

    HTH,
    M4
     
    Martijn Lievaart, Jan 9, 2008
    #9
  10. On Wed, 09 Jan 2008 12:06:35 -0800, wrote:

    > On Jan 9, 2:39 pm, Martijn Lievaart <> wrote:
    >> On Wed, 09 Jan 2008 11:00:18 -0800, wrote:
    >> > I have the following expression in my Perl script:

    >>
    >> > if (/^"([^\,].+)"\,"$/)


    (snip)

    > I might add, the input file looks like this:
    >
    > "#ABC","
    > [
    > ","ABC Dispensing designs, manufactures and services dispensing systems

    (snip)

    Yes, that regex will match the first line.

    $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^\,].+)"\,"$/'
    yes

    (Note extra backslashes in the input line because this is executed from
    the commandline)

    BTW, the backslashes before the commas are completely unneeded.

    $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^,].+)","$/'
    yes

    HTH,
    M4
     
    Martijn Lievaart, Jan 9, 2008
    #10
  11. "John W. Krahn" <> wrote:
    >Jürgen Exner wrote:
    >> Jürgen Exner <> wrote:


    >>> [^\,] one of the three characters caret, backslash, comma

    >>
    >> Ooops, correction. Make that
    >> [^\,] any single character, that is not backslash or comma

    >
    >You were right the first time. A backslash before a normal character in
    >a double quoted string is superfluous.


    Yes, but:
    - this is not a double quoted string but a RE character class. So the
    backslash would be taken literally ARAIR.
    - My mistake was to not consider that ^ at the beginning of a character
    class indicates the negation of that class.

    jue
     
    Jürgen Exner, Jan 9, 2008
    #11
  12. Martijn Lievaart wrote:
    > On Wed, 09 Jan 2008 12:06:35 -0800, wrote:
    >
    >> On Jan 9, 2:39 pm, Martijn Lievaart <> wrote:
    >>> On Wed, 09 Jan 2008 11:00:18 -0800, wrote:
    >>>> I have the following expression in my Perl script:
    >>>> if (/^"([^\,].+)"\,"$/)

    >
    > (snip)
    >
    >> I might add, the input file looks like this:
    >>
    >> "#ABC","
    >> [
    >> ","ABC Dispensing designs, manufactures and services dispensing systems

    > (snip)
    >
    > Yes, that regex will match the first line.
    >
    > $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^\,].+)"\,"$/'
    > yes
    >
    > (Note extra backslashes in the input line because this is executed from
    > the commandline)
    >
    > BTW, the backslashes before the commas are completely unneeded.
    >
    > $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^,].+)","$/'
    > yes


    Actually, none of the backslashes are really needed:

    perl -e 'print qq/yes\n/ if q/"#ABC","/ =~ /^"([^,].+)","$/'



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jan 9, 2008
    #12
  13. Jürgen Exner wrote:
    > "John W. Krahn" <> wrote:
    >> Jürgen Exner wrote:
    >>> Jürgen Exner <> wrote:

    >
    >>>> [^\,] one of the three characters caret, backslash, comma
    >>> Ooops, correction. Make that
    >>> [^\,] any single character, that is not backslash or comma

    >> You were right the first time. A backslash before a normal character in
    >> a double quoted string is superfluous.

    >
    > Yes, but:
    > - this is not a double quoted string but a RE character class. So the
    > backslash would be taken literally ARAIR.


    Unless the regular expression uses single quote delimiters the pattern
    is first interpolated as if it were a double quoted string.

    perldoc perlop

    Read through the sections "Quote and Quote-like Operators", "Regexp
    Quote-Like Operators" and "Gory details of parsing quoted constructs".



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jan 9, 2008
    #13
  14. <> wrote:
    >
    > I have the following expression in my Perl script:

    ^^
    ^^
    > if (/^"([^\,].+)"\,"$/)
    >
    > Although I know that it is checking to see if the string starts with
    > some characters, it is not working for my input file.
    >
    > Can someone explain to me in english what it is doing???



    Since you wrote it, you should be able to do that yourself.

    Or did you mean "in someone else's Perl script" instead?

    :)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 10, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    508
    PerlE
    Jan 30, 2004
  2. Replies:
    6
    Views:
    493
    Joe Smith
    Apr 21, 2004
  3. Aristotle

    Help needed with reg exp please

    Aristotle, Sep 4, 2004, in forum: Perl
    Replies:
    4
    Views:
    497
    Gunnar Hjalmarsson
    Sep 4, 2004
  4. RamaKrishna

    Reg: Help me out pls...

    RamaKrishna, Aug 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    333
    RamaKrishna
    Aug 18, 2003
  5. RamaKrishna

    Reg: Help me out pls, urgent..............

    RamaKrishna, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    RamaKrishna
    Aug 19, 2003
Loading...

Share This Page