How to match any string from a list of strings in regular expressions in python?

后端 未结 5 1336
深忆病人
深忆病人 2020-12-01 10:50

Lets say I have a list of strings,

string_lst = [\'fun\', \'dum\', \'sun\', \'gum\']

I want to make a regular expression, where at a point

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 11:16

    string_lst = ['fun', 'dum', 'sun', 'gum']
    x="I love to have fun."
    
    print re.findall(r"(?=("+'|'.join(string_lst)+r"))",x)
    

    You cannot use match as it will match from start.Use findall instead.

    Output:['fun']

    using search you will get only the first match.So use findall instead.

    Also use lookahead if you have overlapping matches not starting at the same point.

提交回复
热议问题