Regex? Match part of or whole word

前端 未结 5 1962
遥遥无期
遥遥无期 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:48

    Dont know how to match a regex in python, but the regex would be:

    "\bp$|\bpo$|\bpot$|\bpota$|\bpotat$|\bpotato$"
    

    This would match anything from p to potato if its the last word in the string, and also for example not something like "foopotato", if this is what you want.

    The | denotes an alternative, the \b is a "word boundary", so it matches a position (not a character) between a word- and a non-word character. And the $ matches the end of the string (also a position).

提交回复
热议问题