REGEX: capturing on optional groups which fail

Discussion in 'Perl' started by Charles Shannon Hendrix, Jun 14, 2004.

  1. I have been writing some code to parse log files, and I used regular
    expressions to build arrays of fields. Those arrays were inserted
    verbatim into an SQL insert command.

    I assing the results of the regex to an array, like this:

    @array = $line =~ /$rex_extract/x;

    Then I found that some lines had a variable ending. There were three
    possible endings:

    "N" warnings
    "N" errors
    "N" errors, error code = "N"

    At the same time, I want a regex failure on lines like this:

    "N" warnings, "N" errors
    "N" warnings, "N" errors, error code = "N"
    "N" errors, "N" warnings
    "N" errors, "N" warnings, error code = "N"

    I found the following regex works and keeps my array in order so I don't
    have to do ugly array parsing later:


    <expressions for first N non-variable fields snipped>
    (?:
    (?:
    ,\s
    "([0-9]+)" # number of...
    \s
    warnings # warnings
    )?
    |
    (?:
    ,\s
    "([0-9]+)" # number of...
    \s
    errors # errors
    (?: # error code
    ,\s
    error\scode\s=\s"([0-9]+)"
    )?
    )?
    )
    \s*$' # end of line

    Question:

    Do captures in failing non-capturing expressions always generate an
    empty array position? I want to make sure I'm not depending on an
    unreliable side effect.

    The reason I like this is that it preserves the order in my array, so I
    don't have to parse it to see which line ending was found.

    I'm interested in seeing better ways of doing this.

    I would also like a pointer to where this behavior is documented. I've
    not been able to find an explicit mention.



    --
    shannon "AT" widomaker.com -- [governorrhea: a contagious disease that
    spreads from the governor of a state downward through other offices and his
    corporate sponsors]
    Charles Shannon Hendrix, Jun 14, 2004
    #1
    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. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    430
    E. Robert Tisdale
    Jul 31, 2003
  2. Replies:
    3
    Views:
    355
    Peter Hansen
    Jun 10, 2005
  3. Joerg Schuster

    more than 100 capturing groups in a regex

    Joerg Schuster, Oct 24, 2005, in forum: Python
    Replies:
    33
    Views:
    1,061
    Tim Peters
    Oct 27, 2005
  4. pinkisntwell

    OT: GNU regex library and non-capturing groups

    pinkisntwell, Nov 13, 2009, in forum: C Programming
    Replies:
    1
    Views:
    364
    Seebs
    Nov 13, 2009
  5. candide
    Replies:
    6
    Views:
    487
    Devin Jeanpierre
    Jan 3, 2012
Loading...

Share This Page