Regular expression query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Biddiscombe

    #1

    Regular expression query

    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='12a b'"
    "parameter='12a b' biz boz"
    "parameter="12a b""
    "parameter="12a b" 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'pa rameter=(["\']?(.*?)\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>

  • bruno at modulix

    #2
    Re: Regular expression query

    Martin Biddiscombe wrote:[color=blue]
    > It's probably quite simple, but what I want is a regular expression[/color]

    If it's simple, then you probably *dont* want a regexp.
    [color=blue]
    > to
    > parse strings of the form:
    >
    > "parameter=12ab "
    > "parameter= 12ab foo bar"
    > "parameter='12a b'"
    > "parameter='12a b' biz boz"
    > "parameter="12a b""
    > "parameter="12a b" 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'pa rameter=(["\']?(.*?)\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?[/color]

    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.

    [color=blue]
    > Thanks[/color]

    HTH
    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Tim Chase

      #3
      Re: Regular expression query

      > "parameter=12ab "[color=blue]
      > "parameter= 12ab foo bar"
      > "parameter='12a b'"
      > "parameter='12a b' biz boz"
      > "parameter="12a b""
      > "parameter="12a b" 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'pa rameter=(["\']?(.*?)\1( *|$)')[/color]

      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)...thou gh 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=12a b', True),
      ('parameter=12a b foo bar', True),
      ("parameter='12 ab'", True),
      ("parameter='12 ab' biz boz", True),
      ('parameter="12 ab"', True),
      ('parameter="12 ab" junk', True),
      ('parameter="12 ab', False),
      ('parameter=\'1 2ab', False),
      ('parameter="12 ab\'', False),
      ('parameter="12 ab\' foo baz', False)
      ]
      exp = r'parameter=((["\'])(.*?)\2|[^\'" ]+).*'
      r = re.compile(exp)
      print "Using regexp: %s" % exp
      for test,expectedRe sult 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)





      Comment

      • Giovanni Bajo

        #4
        Re: Regular expression query

        Martin Biddiscombe wrote:
        [color=blue]
        > "parameter=12ab "
        > "parameter= 12ab foo bar"
        > "parameter='12a b'"
        > "parameter='12a b' biz boz"
        > "parameter="12a b""
        > "parameter="12a b" junk"[/color]
        [color=blue][color=green][color=darkred]
        >>> import shlex
        >>> def extract(s):[/color][/color][/color]
        .... s = s.split("=")[1]
        .... s = shlex.split(s)[0]
        .... return s
        ....[color=blue][color=green][color=darkred]
        >>> extract("parame ter=12ab")[/color][/color][/color]
        '12ab'[color=blue][color=green][color=darkred]
        >>> extract("parame ter=12ab foo bar")[/color][/color][/color]
        '12ab'[color=blue][color=green][color=darkred]
        >>> extract("parame ter='12ab'")[/color][/color][/color]
        '12ab'[color=blue][color=green][color=darkred]
        >>> extract("parame ter='12ab' biz boz")[/color][/color][/color]
        '12ab'[color=blue][color=green][color=darkred]
        >>> extract('parame ter="12ab"')[/color][/color][/color]
        '12ab'[color=blue][color=green][color=darkred]
        >>> extract('parame ter="12ab" junk')[/color][/color][/color]
        '12ab'

        --
        Giovanni Bajo


        Comment

        • bruno at modulix

          #5
          Re: Regular expression query

          Giovanni Bajo wrote:[color=blue]
          > Martin Biddiscombe wrote:
          >
          >[color=green]
          >>"parameter=12 ab"
          >>"parameter=12 ab foo bar"
          >>"parameter='1 2ab'"
          >>"parameter='1 2ab' biz boz"
          >>"parameter="1 2ab""
          >>"parameter="1 2ab" junk"[/color]
          >
          >[color=green][color=darkred]
          >>>>import shlex
          >>>>def extract(s):[/color][/color]
          >
          > ... s = s.split("=")[1]
          > ... s = shlex.split(s)[0]
          > ... return s[/color]

          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 'onurb@xiludom. gro'.split('@')])"

          Comment

          Working...