473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regex: match whole word

is there a simple way to make it so your regex only matches whole words?

i was thinking simply something like:

[^a-zA-Z0-9]*match_string[^a-zA-Z0-9]*

but then I think it would fail if the word was at the beginning or end of a
line?
Oct 26 '06 #1
5 10554
well...wouldn't

[\w]*

work?

MrNobody wrote:
is there a simple way to make it so your regex only matches whole words?

i was thinking simply something like:

[^a-zA-Z0-9]*match_string[^a-zA-Z0-9]*

but then I think it would fail if the word was at the beginning or end of a
line?
Oct 26 '06 #2
[\w]+ better - match any alphanumeric character group of length 1 or
more (* is zero or more).

ibiza wrote:
well...wouldn't

[\w]*

work?

MrNobody wrote:
is there a simple way to make it so your regex only matches whole words?

i was thinking simply something like:

[^a-zA-Z0-9]*match_string[^a-zA-Z0-9]*

but then I think it would fail if the word was at the beginning or end of a
line?
Oct 26 '06 #3

"MrNobody" <Mr******@discussions.microsoft.comwrote in message
news:5C**********************************@microsof t.com...
is there a simple way to make it so your regex only matches whole words?

i was thinking simply something like:

[^a-zA-Z0-9]*match_string[^a-zA-Z0-9]*

but then I think it would fail if the word was at the beginning or end of
a
line?
You want to use the "negative look-ahead" (and behind) construct.
Oct 26 '06 #4
In article <11*********************@k70g2000cwa.googlegroups. com>,
Martin Z <ma***********@gmail.comwrote:

: ibiza wrote:
:
: well...wouldn't
: >
: [\w]*
: >
: work?
:
: [\w]+ better - match any alphanumeric character group of length 1 or
: more (* is zero or more).

The brackets are redundant: \w+ does the job.

Greg
--
Until these powers are restored -- and the Fed, the income tax, and
the Seventeenth Amendment abolished -- Americans have no hope of ever
returning to a regime of constitutional liberty.
-- Thomas DiLorenzo
Oct 30 '06 #5
In article <5C**********************************@microsoft.co m>,
MrNobody <Mr******@discussions.microsoft.comwrote:

: is there a simple way to make it so your regex only matches whole
: words?
:
: i was thinking simply something like:
:
: [^a-zA-Z0-9]*match_string[^a-zA-Z0-9]*
:
: but then I think it would fail if the word was at the beginning or end
: of a line?

What's your idea of whole words?

You can use \b to match word boundaries, e.g., @"\bwhole words\b".

If I'm not on the right track, please illustrate with examples.

Greg
--
It should be noted that government is never so zealous in suppressing
crime as when that crime consists of direct injury to its own sources of
revenue, as in tax evasion and counterfeiting of its currency.
-- Murray Rothbard
Oct 30 '06 #6

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

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
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,...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
6
by: Lubomir | last post by:
Hi, I am using the following pattern: "\\b" + MySttring + "\\b" If MyString is "one", this should pick up whole words like "one". The problem is, it will pick up also the word: "one.two"...
6
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other...
7
by: Bert | last post by:
hi How do I make a regex to find a word that starts en ends with a certain letter. What is in between is random. thanks b
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.