Meaning of \b in .Net Regular Expressions

9

0

Microsoft has a nifty quick reference card for .Net Regular Expressions.

But it seems to list \b as both matching Backspace and also matching "On word boundary".

Which is it? Can \b really do both? How can you be precise about which one you mean?

abelenky

Posted 2014-06-19T21:35:47.030

Reputation: 771

Question was closed 2014-06-20T06:15:36.863

1I'm not sure why you asked a C#/.Net question here when you have 27k on Stack Overflow where this belongs, topically. – slhck – 2014-06-20T06:16:48.940

The Visual Studio IDE amd Powershell both use .Net expressions. I came across this question in the context of using the IDE. Searching and Editing files is more of a SuperUser task than a programming problem. – abelenky – 2014-06-20T09:04:25.073

I see, thanks. Maybe you can clarify the context, e.g. show a screenshot of the IDE or explain the searching files part? Then I'd say it's fine to stay here. – slhck – 2014-06-20T09:12:01.797

Answers

8

\b means "word boundary" outside of character classes (also called character sets) and "backspace" inside character classes.

Here it means a word boundary:

\bhello\b

Here it means a backspace

[\b]

See this Microsoft reference: Character Escapes in Regular Expressions.
PERL regex has the same definition for \b.

Olivier Jacot-Descombes

Posted 2014-06-19T21:35:47.030

Reputation: 338

Agreed, per this reference: http://msdn.microsoft.com/en-us/library/4edbef7e(v=vs.110).aspx

– Mark Allen – 2014-06-19T21:48:48.840

@MarkAllen: Added your link to my post. Thanks. – Olivier Jacot-Descombes – 2014-06-19T21:58:02.393

From a quick LinqPad check, \b is the only \[a-zA-Z] that is defined for both a standalone backslash and as a character class that returns a different match set. – Mark Hurd – 2014-06-20T03:39:02.047