Regex whitespace word boundary

前端 未结 3 860
醉梦人生
醉梦人生 2020-11-22 02:04

I have this expression

\\b[A-Za-z]+\\b

If I give abc@de mnop, it matches abc, de and mnop

相关标签:
3条回答
  • 2020-11-22 02:23

    \b is a word boundary.

    So, \b is similar to [^a-zA-Z0-9_] i.e \b would check for anything except word

    You can instead use this regex

    (?<=\s|^)[a-zA-Z]+(?=\s|$)
    -------- --------- ------
       |         |       |->match only if the pattern is followed by a space(\s) or end of string/line($)
       |         |->pattern
       |->match only if the pattern is preceded by space(\s) or start of string\line(^)
    
    0 讨论(0)
  • 2020-11-22 02:34

    \b means (?:(?<!\w)(?=\w)|(?<=\w)(?!\w)). Which would match positions between letters and @.

    You can write:

    (?<!\S)[A-Za-z]+(?!\S)
    

    (?!\S) is equivalent to (?=\s|$).

    0 讨论(0)
  • 2020-11-22 02:35

    Regex word boundary doen't match(\b) matching and whitespace

    Only white in sample is

    abc@de mnop   
          ^
    

    Try \s([A-Za-z]+)\b

    where \s is the anchor, not a boundry at all

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