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 ;)
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With