6

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 ;)

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64

3 Answers3

10

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"
Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

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
  • 56,552
  • 12
  • 145
  • 147
1

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

@"\bINSERT\b"

Otherwise the regex sees "bINSERTb".

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Just had this problem, everything was working in my various external RegEx testers; just not in my code - I forgot the @. – ProVega Apr 28 '14 at 03:30