Python regular expression match whole word

后端 未结 4 1059
梦毁少年i
梦毁少年i 2020-11-21 06:05

I\'m having trouble finding the correct regular expression for the scenario below:

Lets say:

a = \"this is a sample\"

I want to mat

4条回答
  •  太阳男子
    2020-11-21 06:42

    Try

    re.search(r'\bis\b', your_string)
    

    From the docs:

    \b Matches the empty string, but only at the beginning or end of a word.

    Note that the re module uses a naive definition of "word" as a "sequence of alphanumeric or underscore characters", where "alphanumeric" depends on locale or unicode options.

    Also note that without the raw string prefix, \b is seen as "backspace" instead of regex word boundary.

提交回复
热议问题