Regular expressions \b but with not just alphanumeric characters in c#

后端 未结 2 2033
悲&欢浪女
悲&欢浪女 2021-01-22 05:35

I want the same functionality as \\b but with other characters.

In C#, I want to have something like

string str = \"\\\\b\" + Regex.Escape(         


        
相关标签:
2条回答
  • 2021-01-22 05:48

    The problem you experience is related to the fact that \b word boundary is context dependent, and \b\(abc\b will match (abc in x(abc) but not in :(abc) (\b\( means there must be a word char before ().

    To match any string that is not enclosed with word chars use

    var pattern = $@"(?<!\w){Regex.Escape(string)}(?!\w)";
    

    See the regex demo.

    Here, (?<!\w) is a negative lookbehind that will make sure there is no word char immediately to the left of the current location, and (?!\w) negative lookahead will make sure there is no word char immediately to the right of the current location.

    Other custom "word" boundaries:

    • Whitespace word boundary: var pattern = $@"(?<!\S){Regex.Escape(string)}(?!\S)"; // Match when enclosed with whitespaces
    • Word and symbol boundary (if you do not want to find c in c++): var pattern = $@"(?<![\w\p{S}]){Regex.Escape(string)}(?![\w\p{S}])";
    0 讨论(0)
  • 2021-01-22 05:52

    For this you'd need a conditional word boundary at each end.
    It just guards the string begin and end, if it's a word, it must be at
    a word boundary.

    If it's not a word, the default is nothing, as it should be.

     (?(?= \w )
          \b 
     )
     (?: @\(Something )
     (?(?<= \w )
          \b 
     )
    

    So, it ends up looking like

    string str = "(?(?=\\w)\\b)" + Regex.Escape(string) + "(?(?<=\\w)\\b)";
    

    Regexstorm.net demo

    This takes the guesswork out of it.

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