views:

124

answers:

3

I have read that to match a word inside of a string using Regular expressions (in .NET), I can use the word boundary specifier (\b) within the regex. However, none of these calls result in any matches

Regex.Match("INSERT INTO TEST(Col1,Col2) VALUES(@p1,@p2)", @"\b@p1\b");

Regex.Match("INSERT INTO TEST(Col1,Col2) VALUES(@p1,@p2)", @"\bINSERT\b");

Is there anything I am doing wrong ?

EDIT: The second one is already working ;)

+7  A: 

Update: As another answer pointed out, @ is not a word character so there is no word boundary between @ and space. As a workaround, you could instead use a negative lookbehind:

@"(?<!\w)@p1\b"

Original answer: You need a @ in front of your regular expressions:

@"\b@p1\b"

Without this, the string "\b" is interpreted as a backspace (character 8), not a regular expression word boundary. There is more information about @-quoted string literals on MSDN.

An alternative way without using @-quoted string literals is to escape your backslashes:

"\\b@p1\\b"
Mark Byers
You're right, I have corrected that, still no matches however...
Thomas Wanner
@Thomas: See update.
Mark Byers
@Mark : Thanks a lot, works like charm :)
Thomas Wanner
A: 

The \ is getting escaped in your strings - you need to use string literals to avoid this:

@"\bINSERT\b"

Otherwise the regex sees "bINSERTb".

Oded
+4  A: 

The second case is solved by @"\bINSERT\b" as stated in another answer.

However /b matches at:

  • Before the first character in the string, if the first character is a word character.
  • After the last character in the string, if the last character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

A word character is one of [a-zA-Z0-9_] so the first case is not solvable by prefixing @ to escape the \b character because you are trying to then match a non word character (@).


Update: The first case can be solved by a negative look-behind assertion but also by using a negated word boundary \B which results in a more cleaner syntax (@"\B@p1\b").

João Angelo
Oh, thanks for pointing that out ;) Is there any way to overcome it ?
Thomas Wanner
@Thomas Wanner, you can use Regular Expressions Lookbehind Zero-Width Assertions. Check Mark Byers answer for a solution.
João Angelo