2

I'm looking for a Regex (using .NET) to match the word ass. The Regex shouldn't match words like assignment.

How can I do this?

Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
Derin
  • 2,045
  • 7
  • 28
  • 31

6 Answers6

5

You are looking for word boundaries (\b):

\bass\b

This will match ass but not bass or assignment.

Daniel Hilgarth
  • 166,158
  • 40
  • 312
  • 426
  • 1
    And, since this is C# (although @Marc removed that tag), take care to use verbatim strings: `@"\bass\b"`. If you forget the `@`, the `\b` means backspace. – Tim Pietzcker May 12 '11 at 09:06
1

How about \bass\b ? This uses word boundaries to limit it to the single word.

Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
1

Most modern regular expression engines support the \b anchor, meaning a zero-width word boundary.

See this page for some examples using that (and other) anchor characters.

paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
1

Visit this page for full manual Word Boundaries

Saleh
  • 2,911
  • 5
  • 33
  • 59
0

Will this work for you?

\bass\b
UserControl
  • 14,181
  • 19
  • 92
  • 181
0

\b(ass)\b or \bass\b should be good.

locoboy
  • 36,684
  • 67
  • 180
  • 255