Regex find word in the string

前端 未结 3 894
孤街浪徒
孤街浪徒 2020-12-28 14:16

In general terms I want to find in the string some substring but only if it is contained there.

I had expression :

^.*(\\bpass\\b)?.*$
3条回答
  •  感情败类
    2020-12-28 14:33

    A period only matches a single character, so you're

    ^.(\bpass\b)?.$
    

    is matching:

    • Start of input
    • A single character
    • Optionally
      • Word boundary
      • "pass"
      • Word boundary
    • Single char
    • End of input

    which I would not expect to match "high pass h3" at all.

    The regular expression:

    pass
    

    (no metacharacters) will match any string containing "pass" (but then so would a "find string in string" function, and this would probably be quicker without the complexities of a regex).

提交回复
热议问题