Why \b does not match word using .net regex

后端 未结 2 493
情歌与酒
情歌与酒 2020-11-27 23:24

To review regular expresions I read this tutorial. Anyways that tutorial mentions that \\b matches a word boundary (between \\w and \\W characters). That tutori

相关标签:
2条回答
  • 2020-11-28 00:18

    The C# language and .NET Regular Expressions both have their own distinct set of backslash-escape sequences, but the C# compiler is intercepting the "\b" in your string and converting it into an ASCII backspace character so the RegEx class never sees it. You need to make your string verbatim (prefix with an at-symbol) or double-escape the 'b' so the backslash is passed to RegEx like so:

    @"\bCOMPILATION UNIT";
    

    Or

    "\\bCOMPILATION UNIT"
    

    I'll say the .NET RegEx documentation does not make this clear. It took me a while to figure this out at first too.

    Fun-fact: The \r and \n characters (carriage-return and line-break respectively) and some others are recognized by both RegEx and the C# language, so the end-result is the same, even if the compiled string is different.

    0 讨论(0)
  • 2020-11-28 00:20

    You should use @"\bCOMPILATION UNIT". This is a verbatim literal. When you do "\b" instead, it parses \b into a special character. You can also do "\\b", whose double backslash is parsed into a real backslash, but it's generally easier to just use verbatims when dealing with regex.

    0 讨论(0)
提交回复
热议问题