473,394 Members | 1,971 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,394 software developers and data experts.

a n00b regex qestion

I am doing a string.replace in a simple table generation app I wrote,
and I can't figure out how to match whitespace with /s, so I thought
I would see if osmeone where would be kind enough to tell me what I am
getting wrong.
This works:

string = string.replace('<tr>\n <th class="table">Field One</th>
\n <td>%FieldOneValue%</td>\n </tr>', '')
You can see I had to actually put in space characters and linefeeds
exactly as they are in the string.

I tried these this:

string = string.replace('<tr>\s*<th class="table">Field One</th>\s*<td>
%FieldOneValue%</td>\s*</tr>', '')
But this doesn't work. The doco for Python's regex suggests that \s
should match any whitespace including newlines which is what I
wanted, but just in case, I also tried this:

string = string.replace('<tr>\n\s*<th class="table">Field One</th>\n
\s*<td>%FieldOneValue%</td>\n\s*</tr>', '')
Any help explaining why these are not working would be greatly
appreciated.

TIA

nuffi
Dec 3 '07 #1
2 1469
nu*******@gmail.com a écrit :
I am doing a string.replace in a simple table generation app I wrote,
and I can't figure out how to match whitespace with /s,
Hahem... Where did you get the idea that str.replace would work with
regexps ?

"""
replace(...)
S.replace (old, new[, count]) -string

Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
"""

See any mention of regexps here ?-)
so I thought
I would see if osmeone where would be kind enough to tell me what I am
getting wrong.
This works:

string = string.replace('<tr>\n <th class="table">Field One</th>
\n <td>%FieldOneValue%</td>\n </tr>', '')
You can see I had to actually put in space characters and linefeeds
exactly as they are in the string.
Indeed. That's consistent with the doc.
I tried these this:

string = string.replace('<tr>\s*<th class="table">Field One</th>\s*<td>
%FieldOneValue%</td>\s*</tr>', '')
But this doesn't work.
It works if you have *litteraly* the first arg in your string !-)
The doco for Python's regex suggests that \s
should match any whitespace including newlines which is what I
wanted,
So why do you insist on using str.replace insted of re.sub ?-)
but just in case, I also tried this:

string = string.replace('<tr>\n\s*<th class="table">Field One</th>\n
\s*<td>%FieldOneValue%</td>\n\s*</tr>', '')
Any help explaining why these are not working would be greatly
appreciated.
import re
pat = r'<tr>\s*<th class="table">Field One</th>\s*<td>' \
+ r'%FieldOneValue%</td>\s*</tr>'
string = re.sub(pat, '', string)

HTH
Dec 3 '07 #2
I tried these this:
>
string = string.replace('<tr>\s*<th class="table">Field One</th>\s*<td>
%FieldOneValue%</td>\s*</tr>', '')
But this doesn't work. The doco for Python's regex suggests that \s
should match any whitespace including newlines which is what I
wanted,
from http://docs.python.org/lib/module-re.html

"""
Regular expressions use the backslash character ("\") to indicate
special forms or to allow special characters to be used without
invoking their special meaning. This collides with Python's usage
of the same character for the same purpose in string literals;
for example, to match a literal backslash, one might have to
write '\\\\' as the pattern string, because the regular
expression must be "\\", and each backslash must be expressed as
"\\" inside a regular Python string literal.

The solution is to use Python's raw string notation for regular
expression patterns; backslashes are not handled in any special
way in a string literal prefixed with "r". So r"\n" is a
two-character string containing "\" and "n", while "\n" is a
one-character string containing a newline. Usually patterns will
be expressed in Python code using this raw string notation.
"""

and from http://docs.python.org/lib/re-syntax.html

"""
If you're not using a raw string to express the pattern, remember
that Python also uses the backslash as an escape sequence in
string literals; if the escape sequence isn't recognized by
Python's parser, the backslash and subsequent character are
included in the resulting string. However, if Python would
recognize the resulting sequence, the backslash should be
repeated twice. This is complicated and hard to understand, so
it's highly recommended that you use raw strings for all but the
simplest expressions.
"""

And if you don't know about raw strings, you can read about them
here:

http://docs.python.org/ref/strings.html

-tkc

Dec 3 '07 #3

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

Similar topics

1
by: Matt | last post by:
I'd like to overwrite just one line of a binary file, based on a position set by seek(). Is there no way to do this? As far as I can tell I need to read the whole file, change the line, and write...
3
by: Anupam Kapoor | last post by:
hi all, a python n00b, so please bear with me. i have a simple question: i generally name python sources as a-simple-python-example.py. when i try to import a module named as above, i...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
1
by: newgenre | last post by:
I am using a pre-built package of code for my site, which is called EasyDisc. All it does is it creates an interactive forum on your site, like any forum you see anywhere. I am having a problem...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.