Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > \number parameter in regular expression

Reply
Thread Tools

\number parameter in regular expression

 
 
Piet
Guest
Posts: n/a
 
      09-25-2004
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
 
Reply With Quote
 
 
 
 
Michael Hoffman
Guest
Posts: n/a
 
      09-25-2004
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
 
Reply With Quote
 
 
 
 
Michael Fuhr
Guest
Posts: n/a
 
      09-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments