Regex? Match part of or whole word

前端 未结 5 1955
遥遥无期
遥遥无期 2021-01-25 23:33

I was wondering if it\'s possible to use regex with python to capture a word, or a part of the word (if it\'s at the end of the string).

Eg:
target word - potato

5条回答
  •  失恋的感觉
    2021-01-25 23:58

    No, you can't do that with a regex as far as I know, without pointless (p|po|pot ...) matches which are excessive. Instead, just pick off the last word, and match that using a substring:

    match = re.search('\S+$', haystack)
    if match.group(0) == needle[:len(match.group(0))]:
        # matches.
    

提交回复
热议问题