data regex match

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

    #1

    data regex match

    Hi

    I am having an issue with this match

    tx = "now 04/30/2006 then"
    data = re.compile('(\d {2})/\1/\1\1', re.IGNORECASE)
    d = data.search(tx)
    print d

    Nono
    I was expecting 04/30/2006, what went wrong?

    thanks
  • Fredrik Lundh

    #2
    Re: data regex match

    Gary Wessle wrote:
    [color=blue]
    > I am having an issue with this match
    >
    > tx = "now 04/30/2006 then"
    > data = re.compile('(\d {2})/\1/\1\1', re.IGNORECASE)
    > d = data.search(tx)
    > print d
    >
    > Nono
    > I was expecting 04/30/2006[/color]

    really? your pattern matches two digits, followed by a slash, followed
    by a byte with the ASCII value 1, followed by a slash, followed by two
    bytes with the ASCII value 1.
    [color=blue][color=green][color=darkred]
    >>> '(\d{2})/\1/\1\1'[/color][/color][/color]
    '(\\d{2})/\x01/\x01\x01'

    in case you meant to write

    r'(\d{2})/\1/\1\1'

    (which is the same thing as '(\\d{2})/\\1/\\1\\1')

    it's still not close; that pattern matches two digits, followed by a slash,
    followed by the *same* two digits, followed by a slash, followed by the
    same two digits, followed by the same two digits.

    in other words, dates like 20/20/2020 and 12/12/1212.

    try

    '\d\d/\d\d/\d\d\d\d'

    instead.
    [color=blue]
    > what went wrong?[/color]

    (insert obligatory jwz quote here)

    </F>



    Comment

    • Heiko Wundram

      #3
      Re: data regex match

      Am Dienstag 02 Mai 2006 23:06 schrieb Gary Wessle:[color=blue]
      > Hi
      >
      > I am having an issue with this match
      >
      > tx = "now 04/30/2006 then"
      > data = re.compile('(\d {2})/\1/\1\1', re.IGNORECASE)[/color]

      As always, use a raw string for regular expressions. \d is being interpreted
      to mean an ascii character, and not to mean the character class you're trying
      to reference here.

      Second: \1 (if properly quoted in the string) matches the first group exactly.
      Your regex would only match 20/20/2020 or strings of such format.

      Third: IGNORECASE is irrelevant here, you're not trying to match letters, are
      you?

      Anyway, the following works:

      dateregex = re.compile(r"(\ d{2})/(\d{2})/(\d{4})")
      m = dateregex.searc h(tx)
      if m:
      print m.groups()
      else:
      print "No match."

      --- Heiko.

      Comment

      • Rene Pijlman

        #4
        Re: data regex match

        Gary Wessle:[color=blue]
        >tx = "now 04/30/2006 then"
        >data = re.compile('(\d {2})/\1/\1\1', re.IGNORECASE)
        >d = data.search(tx)
        >print d
        >
        >Nono
        >I was expecting 04/30/2006[/color]

        You should expect: NameError: name 're' is not defined
        [color=blue]
        > what went wrong?[/color]

        \1 matches the content of the first group, which is '04'. It doesn't match
        '30', '20' and '06'.

        Also, you'll need to use a raw string, or backslash your backslashes.
        [color=blue][color=green][color=darkred]
        >>> import re
        >>> tx = "now 04/04/0404 then"
        >>> data = re.compile(r'(\ d{2})/\1/\1\1', re.IGNORECASE)
        >>> d = data.search(tx)
        >>> print d[/color][/color][/color]
        <_sre.SRE_Mat ch object at 0x01287F60>

        --
        René Pijlman

        Comment

        • Heiko Wundram

          #5
          Re: data regex match

          Am Dienstag 02 Mai 2006 23:23 schrieb Fredrik Lundh:[color=blue]
          > Gary Wessle wrote:[color=green]
          > > regular expression match: what went wrong?[/color]
          > (insert obligatory jwz quote here)[/color]

          QOTW! :-D

          --- Heiko.

          Comment

          • Fredrik Lundh

            #6
            Re: data regex match

            Heiko Wundram wrote:
            [color=blue]
            > As always, use a raw string for regular expressions. \d is being interpreted
            > to mean an ascii character, and not to mean the character class you're trying
            > to reference here.[/color]

            \d isn't an ASCII character, but \1 is.
            [color=blue][color=green][color=darkred]
            >>> print '(\d{2})/\1/\1\1'[/color][/color][/color]
            (\d{2})/?/??

            </F>



            Comment

            • Heiko Wundram

              #7
              Re: data regex match

              Am Dienstag 02 Mai 2006 23:34 schrieb Fredrik Lundh:[color=blue]
              > Heiko Wundram wrote:[color=green]
              > > As always, use a raw string for regular expressions. \d is being
              > > interpreted to mean an ascii character, and not to mean the character
              > > class you're trying to reference here.[/color]
              >
              > \d isn't an ASCII character, but \1 is.
              >[color=green][color=darkred]
              > >>> print '(\d{2})/\1/\1\1'[/color][/color][/color]

              I tried that just know. Didn't know that \[a-z] weren't all interpreted as
              escape sequences... Seems like I learn something every day. ;-)

              --- Heiko.

              Comment

              Working...