Regular expression query

Discussion in 'Python' started by Martin Biddiscombe, Feb 3, 2006.

  1. It's probably quite simple, but what I want is a regular expression to
    parse strings of the form:

    "parameter=12ab"
    "parameter=12ab foo bar"
    "parameter='12ab'"
    "parameter='12ab' biz boz"
    "parameter="12ab""
    "parameter="12ab" junk"

    in each case returning 12ab as a match. "parameter" is known and fixed.
    The parameter value may or may not be enclosed in single or double
    quotes, and may or may not be the last thing on the line. If the value
    is quoted, it may contain spaces.

    I've tried a regex of the form:
    re.compile(r'parameter=(["\']?(.*?)\1( *|$)')

    This works fine when the parameter's value is quoted, but if the quotes
    are missing, it falls over since the \1 is empty and so the non-greedy
    "match anything" ends up matching nothing.

    Any suggestions?

    Thanks

    <M>
     
    Martin Biddiscombe, Feb 3, 2006
    #1
    1. Advertising

  2. Martin Biddiscombe wrote:
    > It's probably quite simple, but what I want is a regular expression


    If it's simple, then you probably *dont* want a regexp.

    > to
    > parse strings of the form:
    >
    > "parameter=12ab"
    > "parameter=12ab foo bar"
    > "parameter='12ab'"
    > "parameter='12ab' biz boz"
    > "parameter="12ab""
    > "parameter="12ab" junk"
    >
    > in each case returning 12ab as a match. "parameter" is known and fixed.
    > The parameter value may or may not be enclosed in single or double
    > quotes, and may or may not be the last thing on the line. If the value
    > is quoted, it may contain spaces.
    >
    > I've tried a regex of the form:
    > re.compile(r'parameter=(["\']?(.*?)\1( *|$)')
    >
    > This works fine when the parameter's value is quoted, but if the quotes
    > are missing, it falls over since the \1 is empty and so the non-greedy
    > "match anything" ends up matching nothing.
    >
    > Any suggestions?


    yes : forget regexps, use str methods.

    parse = lambda l: \ l.split('=',1)[1].split()[0].strip().strip("'\"")

    NB : I tried my best to make it as obfuscated as a regexp so you still
    gain extra bonus points from Perl-addicts !-p - but feel free to rewrite
    this cleanly.


    > Thanks


    HTH
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Feb 3, 2006
    #2
    1. Advertising

  3. Martin Biddiscombe

    Tim Chase Guest

    > "parameter=12ab"
    > "parameter=12ab foo bar"
    > "parameter='12ab'"
    > "parameter='12ab' biz boz"
    > "parameter="12ab""
    > "parameter="12ab" junk"
    >
    > in each case returning 12ab as a match. "parameter" is known and fixed.
    > The parameter value may or may not be enclosed in single or double
    > quotes, and may or may not be the last thing on the line. If the value
    > is quoted, it may contain spaces.
    >
    > I've tried a regex of the form:
    > re.compile(r'parameter=(["\']?(.*?)\1( *|$)')


    Below is a test-harness that seemed to spit out the results you
    want (I threw in some bogus tests to make sure they failed too)
    with the given value for "exp".

    The resulting match object will have your desired value in
    group(1)...though it will include whatever quotes happened to be
    in it. You may also need to anchor accordingly with "^" and "$"

    It doesn't gracefully handle escaped quotes in your value

    -tim


    import re
    tests = [
    ('parameter=12ab', True),
    ('parameter=12ab foo bar', True),
    ("parameter='12ab'", True),
    ("parameter='12ab' biz boz", True),
    ('parameter="12ab"', True),
    ('parameter="12ab" junk', True),
    ('parameter="12ab', False),
    ('parameter=\'12ab', False),
    ('parameter="12ab\'', False),
    ('parameter="12ab\' foo baz', False)
    ]
    exp = r'parameter=((["\'])(.*?)\2|[^\'" ]+).*'
    r = re.compile(exp)
    print "Using regexp: %s" % exp
    for test,expectedResult in tests:
    if r.match(test):
    result = True
    else:
    result = False
    if result == expectedResult:
    print "[%s] passed" % test
    else:
    print "[%s] failed (expected %s, got %s)" % (test,
    expectedResult, result)
     
    Tim Chase, Feb 3, 2006
    #3
  4. Martin Biddiscombe wrote:

    > "parameter=12ab"
    > "parameter=12ab foo bar"
    > "parameter='12ab'"
    > "parameter='12ab' biz boz"
    > "parameter="12ab""
    > "parameter="12ab" junk"


    >>> import shlex
    >>> def extract(s):

    .... s = s.split("=")[1]
    .... s = shlex.split(s)[0]
    .... return s
    ....
    >>> extract("parameter=12ab")

    '12ab'
    >>> extract("parameter=12ab foo bar")

    '12ab'
    >>> extract("parameter='12ab'")

    '12ab'
    >>> extract("parameter='12ab' biz boz")

    '12ab'
    >>> extract('parameter="12ab"')

    '12ab'
    >>> extract('parameter="12ab" junk')

    '12ab'

    --
    Giovanni Bajo
     
    Giovanni Bajo, Feb 4, 2006
    #4
  5. Giovanni Bajo wrote:
    > Martin Biddiscombe wrote:
    >
    >
    >>"parameter=12ab"
    >>"parameter=12ab foo bar"
    >>"parameter='12ab'"
    >>"parameter='12ab' biz boz"
    >>"parameter="12ab""
    >>"parameter="12ab" junk"

    >
    >
    >>>>import shlex
    >>>>def extract(s):

    >
    > ... s = s.split("=")[1]
    > ... s = shlex.split(s)[0]
    > ... return s


    I definitevely have to learn and use the shlex module.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Feb 6, 2006
    #5
    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. lonelyplanet999

    Regular Expression Query

    lonelyplanet999, Aug 14, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,854
  2. VSK
    Replies:
    2
    Views:
    2,400
  3. Sriv Chakravarthy
    Replies:
    0
    Views:
    837
    Sriv Chakravarthy
    Sep 18, 2003
  4. Replies:
    6
    Views:
    670
  5. George

    Regular Expression Query

    George, Jan 24, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    123
    Gunnar Hjalmarsson
    Jan 24, 2005
Loading...

Share This Page