Regex in VS.net

bsleek

Newcomer
Joined
Dec 29, 2004
Messages
4
I'm having trouble matching word boundaries in VS. I'm using the Find tool with Use Regular Expressions enabled. I'm unable to match on words such as... \bmyvariable\b
However line anchors such as ^, and $ work fine. Anyone have any ideas what I'm doing wrong?

Ben
 
Word boundaries

bsleek said:
I'm having trouble matching word boundaries in VS. I'm using the Find tool with Use Regular Expressions enabled. I'm unable to match on words such as... \bmyvariable\b
However line anchors such as ^, and $ work fine. Anyone have any ideas what I'm doing wrong?

Ben

Hey Ben,

I have not seen the \b symbol in regular expressions before. Are you trying to use that as a "boundary" indicator? One expression you can use is

\W\w+\W

which will find "words" using the following method:

\W means a "non-word" character
\w+ means one or more "word" characters

so the expression above says find

a non-word character followed by one or more word characters followed by a non-word character.

If you want to find a specific word bounded by non-word characters you can use

\Wmyvariable\W

Let me know if this helps or if I didn't really understand your question. :)
 
bsleek said:
Yes, word boundaries are what I was getting. When learning regexes, several sites speak of word boundary anchors such as \b that match between characters (zero length). I know this site...

http://www.regular-expressions.info/wordboundaries.html

As well as others speak of word boundaries. Is there a similar way to match using the VS regex engine?

I finally saw my problem. If you extend the little > arrow at the end of the Search field you see SURPRISE the metacharacters used by .net regex engine. So the equivalent \b word boundary is the metacharacter > for and end String or < for a beginning String.
 
Excellent!

bsleek said:
I finally saw my problem. If you extend the little > arrow at the end of the Search field you see SURPRISE the metacharacters used by .net regex engine. So the equivalent \b word boundary is the metacharacter > for and end String or < for a beginning String.

Yeah...I've got Visual Studio .NET 2003 and I love the auto expand cabability. I went to the link you had above and read about the \b metacharacter. It seems to be a Perl usage. It is cool because using it allows you to grab only the word itself, without having to strip off the leading and/or trailing boundary character as in the example I gave. I must confess that I have not used regex in .NET much yet. Thanks for your posting the question because now my mind has expanded to include more regex stuff. :)
 
Back
Top