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

how to match whole word

Hi,

The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?

Thanks,
Peng

if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
Jul 16 '08 #1
5 5336
Peng Yu wrote:
Hi,

The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?

Thanks,
Peng

if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
http://mail.python.org/mailman/listinfo/python-list
The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html

Gary Herron
Jul 16 '08 #2
On Jul 15, 10:29 pm, Gary Herron <gher...@islandtraining.comwrote:
Peng Yu wrote:
Hi,
The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?
Thanks,
Peng
if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
http://mail.python.org/mailman/listinfo/python-list

The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html

Gary Herron
I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.

Thanks,
Peng
Jul 16 '08 #3
Peng Yu wrote:
I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.
as the documentation says, \A and \Z matches at the beginning/end of a
*string*, not a word.

</F>

Jul 16 '08 #4
On Jul 16, 9:38 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 15, 10:29 pm, Gary Herron <gher...@islandtraining.comwrote:
Peng Yu wrote:
Hi,
The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?
Thanks,
Peng
if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + r"(?=\s|
$)",
opts.ignore_case and re.I or 0)
--
>http://mail.python.org/mailman/listinfo/python-list
The regular expression "\w+" will match (what might be your definition
of) a word, and in particular will match abc in :abc:. Regular
expressions have lots of other special \-sequences that might be worth
your while to read about: http://docs.python.org/lib/re-syntax.html
Gary Herron

I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.

Thanks,
Peng
Not sure why you picked \A and \Z -- they are only useful if you are
using the re.M flag.
What you want is \b -- match word boundary, on either side of your
word:

regex = re.compile(r"\b" + re.escape(old_str) + r"\b",re.I)

re.I is the same as re.IGNORECASE. More than one option may be OR'ed
together. There's no such thing as "re.O" in Python. I can understand
where you get the idea, as there is an 'o' modifier for REs in Perl.

To summarize, \A and \Z match the beginning and end of a STRING, while
\b matches the beginning or end of a WORD.

-- john
Jul 16 '08 #5
John S wrote:
Not sure why you picked \A and \Z -- they are only useful if you are
using the re.M flag.
Well, they're aliases for ^ and $ in "normal" mode, at least for strings
that don't end with a newline.
re.I is the same as re.IGNORECASE. More than one option may be OR'ed
together. There's no such thing as "re.O" in Python. I can understand
where you get the idea, as there is an 'o' modifier for REs in Perl.
His code did

opts.ignore_case and re.I or 0

which is the same as "re.I if opts.ignore_case else 0" in Python 2.5,
where 0 is a zero and not an O.

</F>

Jul 16 '08 #6

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

Similar topics

4
by: Gawelek | last post by:
Lat say, we have such a string : "Ala ma kota" Is is possible to express using Regular Expresion, that I want to get word "kot", that lies behind word "ma" ? BUT, it is the most important thing,...
2
by: ShadowOfTheBeast | last post by:
Hello All, i have been cracking my skull on how to seach a particular reg exp match within a string? it does not seem to happen except the whole arbitrary string is the exact match of the regular...
1
by: Buddhist[CHinA] | last post by:
I mean I wanna search a string in certain text files. But I don't know how to effectively get a whole-word match result. Can someone do me a favor? thx.
5
by: MrNobody | last post by:
is there a simple way to make it so your regex only matches whole words? i was thinking simply something like: *match_string* but then I think it would fail if the word was at the beginning...
6
by: lawrence k | last post by:
Wierd. Go to this page: http://www.ihanuman.com/search.php and search for "yoga" This query gets run: SELECT * FROM albums WHERE MATCH(name,description) AGAINST ('yoga') ORDER BY id DESC
3
by: Ethan Strauss | last post by:
Hi, I have written a regular expression which is supposed to pull a direction (forward or reverse) designation from a file name. Unfortunately, the direction designation can either be the...
4
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that...
6
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Le Thursday 26 June 2008 15:53:06 oyster, vous avez écrit : The construct does not match a whole word but only one char, so means "any char which is not t, a, b, l or e". Anyway the inside...
5
by: nse111 | last post by:
Hey yaaaaaaa guys n gals! I want to know how I can match a whole word within a string using php. these are the results returned by the mysql query: tree:sweet:house:gate:tent...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.