Python regular expression match whole word

后端 未结 4 1075
梦毁少年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:28

    The trouble with regex is that if hte string you want to search for in another string has regex characters it gets complicated. any string with brackets will fail.

    This code will find a word

     word="is"
        srchedStr="this is a sample"
        if srchedStr.find(" "+word+" ") >=0  or \
           srchedStr.endswith(" "+word):
            
    

    The first part of the conditional searches for the text with a space on each side and the second part catches the end of string situation. Note that the endwith is boolean whereas the find returns an integer

提交回复
热议问题