Regex expression to match whole word ?

前端 未结 1 1985
抹茶落季
抹茶落季 2020-12-04 01:29

In reference to my question Regex expression to match whole word with special characters not working ?,

I got an answer which said

@\"(?<=^|\\s)\         


        
相关标签:
1条回答
  • 2020-12-04 01:50

    Try this

    (?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))this (?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))
    

    See it here on Regexr

    This will also work for pattern that starts with a whitespace.

    Basically, what I am doing is to define a custom "word" boundary. But it is not true on a \W=>\w or a \w=>\W change, its true on a \S=>\s or a \s=>\S change!

    Here is an example in c#:

    string str = "Hi this is stackoverflow";
    string pattern = Regex.Escape("this");
    MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + pattern + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", RegexOptions.IgnoreCase);
    
    Console.WriteLine("Amount of matches: " + result.Count);
    foreach (Match m in result)
    {
        Console.WriteLine("Matched: " + result[0]);
    }
    Console.ReadLine();
    

    Update:

    This "Whitespace" boundary can be done more general, so that on each side of the pattern is the same expression, like this

    (?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))
    

    In c#:

    MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))" + pattern + @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))", RegexOptions.IgnoreCase);
    
    0 讨论(0)
提交回复
热议问题