473,760 Members | 10,633 Online
Bytes | Software Development & Data Engineering Community
+ 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 10574
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******@discu ssions.microsof t.comwrote in message
news:5C******** *************** ***********@mic rosoft.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************ *********@k70g2 000cwa.googlegr oups.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************ *************** *******@microso ft.com>,
MrNobody <Mr******@discu ssions.microsof t.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
5728
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 is to improve, but not to prove.
4
3882
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, in "matches" I want to get only word "kot". Example : String s = "Ala ma kota"; Regex r = new Regex("match_but_not_include_word_ma\\skota");
7
1914
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 starting and ending with a *. I tried things like string regex = @"(^*\|*^)"); but it still doesn't work completely (matches on "*test" too for example). If anything could help me with this, that would be appreciated.
17
3979
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 http://forta.com/books/0672325667/
4
3609
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 "LOT:". Source text may contain multiple "SUB: ... LOT:" blocks. For example this is my source text:
6
5551
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" How should I modify the patter to pickup only "one"?
6
9667
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 words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
7
1161
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
12205
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 is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
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 we have to send another system
3
3442
muto222
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.