raw strings in regexps

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

    #1

    raw strings in regexps

    I've been having trouble with a regular expression, and I finally simplified
    things down to the point that (a) my example is very simple, and (b) I'm
    totally confused. There are those who would say (b) is normal, but that's
    another thread.

    I finally simplified my problem down to this simple case:

    re.match(r'\\th is', r'\\this')

    Both the pattern and the string to match are identical raw strings, yet they
    don't match. What does match is this:

    re.match(r'\\\\ this', r'\\this')

    Below are outputs from two versions of Python on two different machines,
    with identical outputs, so it's probably not a compiler problem or a version
    bug.

    What's going on here? Am I missing something obvious?

    linux25python
    Python 2.2.3 (#1, Feb 2 2005, 12:22:48)
    [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>import re
    >>print re.match(r'\\th is', r'\\this')
    None
    >>print re.match(r'\\\\ this', r'\\this')
    <_sre.SRE_Mat ch object at 0x6bf0e0>
    >>>
    C:\Dev\python>p ython
    ActivePython 2.4.2 Build 10 (ActiveState Corp.) based on
    Python 2.4.2 (#67, Jan 17 2006, 15:36:03) [MSC v.1310 32 bit (Intel)] on
    win32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>import re
    >>print re.match(r'\\th is', r'\\this')
    None
    >>print re.match(r'\\\\ this', r'\\this')
    <_sre.SRE_Mat ch object at 0x009DA058>
    >>>
    -- Mike --


  • Carl Banks

    #2
    Re: raw strings in regexps

    Mike wrote:
    I've been having trouble with a regular expression, and I finally simplified
    things down to the point that (a) my example is very simple, and (b) I'm
    totally confused. There are those who would say (b) is normal, but that's
    another thread.
    >
    I finally simplified my problem down to this simple case:
    >
    re.match(r'\\th is', r'\\this')
    >
    Both the pattern and the string to match are identical raw strings, yet they
    don't match.
    In the pattern, the first backslash escapes the second; thus the two
    backslashes in the pattern match one backslash in the string. Another
    example:

    re.match(r"\[",r"\[") =False
    re.match(r"\[",r"[") =True


    Carl Banks

    Comment

    • Fredrik Lundh

      #3
      Re: raw strings in regexps

      Mike wrote:
      I've been having trouble with a regular expression, and I finally simplified
      things down to the point that (a) my example is very simple, and (b) I'm
      totally confused. There are those who would say (b) is normal, but that's
      another thread.
      >
      I finally simplified my problem down to this simple case:
      >
      re.match(r'\\th is', r'\\this')
      >
      Both the pattern and the string to match are identical raw strings, yet they
      don't match.
      the regular expression engine matches a pattern against a string, not a
      string against a string. in a pattern, "\\" matches a single backslash.
      in your raw string, you have *two* backslashes. try this instead:

      re.match(r'\\th is', '\\this')

      </F>

      Comment

      • Gabriel Genellina

        #4
        Re: raw strings in regexps

        At Friday 8/12/2006 02:53, Mike wrote:
        >I finally simplified my problem down to this simple case:
        >
        re.match(r'\\th is', r'\\this')
        >
        >Both the pattern and the string to match are identical raw strings, yet they
        >don't match. What does match is this:
        >
        re.match(r'\\\\ this', r'\\this')
        Perhaps you can understand better with a simpler example without backslashes:
        >>print re.match('(a)', '(a)')
        None
        >>print re.match(r'\(a\ )', '(a)')
        <_sre.SRE_Mat ch object at 0x00C5CDB0>

        You have to quote metacharacters if you want to match them. The
        escape method is useful for this:
        >>re.escape('(a )')
        '\\(a\\)'


        --
        Gabriel Genellina
        Softlab SRL

        _______________ _______________ _______________ _____
        Correo Yahoo!
        Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
        ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

        Comment

        • Mike

          #5
          Re: raw strings in regexps

          "Gabriel Genellina" <gagsl-py@yahoo.com.ar wrote in message
          news:mailman.12 88.1165563291.3 2031.python-list@python.org ...

          ....
          You have to quote metacharacters if you want to match them. The escape
          method is useful for this:
          >
          >re.escape('(a) ')
          '\\(a\\)'
          Doh! Of course! Thanks everyone.

          -- Mike --


          Comment

          Working...