\number parameter in regular expression

Discussion in 'Python' started by Piet, Sep 25, 2004.

  1. Piet

    Piet Guest

    Hi there,
    I am trying to understand the correct use of the \number parameter in
    pythons regular expressions. I belive that the following should work
    import re
    pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")
    search = pattern.search("Date#ThisIsASpacer2004-09-25Date")
    print search.groups()
    But it doesn´t, because the RE doesn´t match.
    I thought that the "\1" thing indicates that the respective part of
    the string in question should match the one that is located in
    search.group(1). When I change the script to
    import re
    pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})")
    search = pattern.search("Date#ThisIsASpacer2004-09-25")
    print search.groups()
    print search.group(1)
    I get the expected result:
    ("Date","#ThisIsASpacer","2004-09-25")
    Date
    How is the \number parameter used correctly?
    Many thanks in advance
    Piet
     
    Piet, Sep 25, 2004
    #1
    1. Advertising

  2. Piet wrote:

    > import re
    > pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")


    You should be using a raw string, r"([a-z...". As a purely stylistic
    concern, I found that calling this regex object "pattern" somewhat
    confusing since that is normally what one calls the string that is
    compiled into a regex.

    regex = re.compile(r"([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")
    ^

    >>> regex.search("Date#ThisIsASpacer2004-09-25Date").groups()

    ('Date', '#ThisIsASpacer', '2004-09-25')
    --
    Michael Hoffman
     
    Michael Hoffman, Sep 25, 2004
    #2
    1. Advertising

  3. Piet

    Michael Fuhr Guest

    (Piet) writes:

    > I am trying to understand the correct use of the \number parameter in
    > pythons regular expressions. I belive that the following should work
    > import re
    > pattern = re.compile("([a-zA-Z]*)(.*)(\d{4}-\d{2}-\d{2})\1")
    > search = pattern.search("Date#ThisIsASpacer2004-09-25Date")
    > print search.groups()
    > But it doesn't, because the RE doesn't match.
    > I thought that the "\1" thing indicates that the respective part of
    > the string in question should match the one that is located in
    > search.group(1).


    Read the intro to the re documentation:

    http://docs.python.org/lib/module-re.html

    Hint: what does \1 mean in a string that's not used as a regular
    expression?

    --
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
     
    Michael Fuhr, Sep 25, 2004
    #3
    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. Ole
    Replies:
    0
    Views:
    5,243
  2. joemono
    Replies:
    5
    Views:
    13,998
    Roy Johnson
    Oct 3, 2003
  3. VSK
    Replies:
    2
    Views:
    2,392
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    886
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    12,053
    vforvikash
    Dec 29, 2008
Loading...

Share This Page