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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Derin
  • 2,125
  • 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
  • 171,043
  • 40
  • 335
  • 443
  • 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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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
  • 854,327
  • 234
  • 1,573
  • 1,953
1

Visit this page for full manual Word Boundaries

Saleh
  • 2,982
  • 5
  • 34
  • 59
0

Will this work for you?

\bass\b
UserControl
  • 14,766
  • 20
  • 100
  • 187
0

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

locoboy
  • 38,002
  • 70
  • 184
  • 260