473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

escapes in regular expressions

I was helping a guy at work with regular expressions and found
something I didn't expect:
re.match('\d', '7').group() '7' re.match('\\d', '7').group() '7'


It's not clear to me why these are the same. Could someone please
explain?

May 21 '06 #1
3 1058
James Thiele schrieb:
I was helping a guy at work with regular expressions and found
something I didn't expect:

re.match('\d', '7').group()
'7'
'\d' is not recognized as a escape sequence by Python and therefore it
is left unchanged <http://docs.python.org/ref/strings.html> in the
string which is passed to re.match.
re.match('\\d', '7').group()


'7'
[...]


This is the correct version. The first backslash escapes the second one
and this version will work even if a future version of Python recognizes
\d as an escape sequence.
Dennis
May 21 '06 #2
"James Thiele" <ja****************@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
I was helping a guy at work with regular expressions and found
something I didn't expect:
re.match('\d', '7').group() '7' re.match('\\d', '7').group() '7'
It's not clear to me why these are the same. Could someone please
explain?


This is not a feature of regexp's at all, but of Python strings. If the
backslash precedes a character that is not normally interpreted, then it is
treated like just a backslash. Look at this sample from the Python command
line:
s = "\d"
s '\\d' s = "\t"
s '\t'
This is one reason why Python programmers who use regexp's use the "raw"
notation to create strings (this is often misnomered as a "raw string", but
the resulting string is an ordinary string in every respect - what is "raw"
about it is the disabling of escape behavior of any backslashes that are not
the last character in the string). It is painful enough to litter your
regexp with backslashes, just because you have the misfortune of having to
match a '.', '+', '?', '*', or brackets or parentheses in your expression,
without having to double up the backslashes for escaping purposes. Consider
these sample statements:
"\d" == "\\d" True "\t" == "\\t" False r"\t" == "\\t" True


So your question is really a string question - you just happened to trip
over it while defining a regexp.

-- Paul
May 21 '06 #3
Am Sonntag 21 Mai 2006 19:49 schrieb James Thiele:
re.match('\d', '7').group() print '\d' \d
re.match('\\d', '7').group() print '\\d'

\d

'\d' evaluates to \d, because d is not a valid escape sequence. '\n' evaluates
to newline, because n is a valid escape sequence. '\\' evaluates to \,
because \ is a valid escape sequence.

--- Heiko.
May 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.