Python regex to match words not having dot

前端 未结 5 1240
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 15:38

I want to accept only those strings having the pattern \'wild.flower\', \'pink.flower\',...i.e any word preceding \'.flower\', but the word should not contain dot. For example,

5条回答
  •  攒了一身酷
    2021-01-26 16:43

    Your case of pink.blue.flower is unclear. There are 2 possibilities:

    • Match only blue (cut off preceding dot and what was before).
    • Reject this case altogether (you want to match a word preceding .flower only if it is not preceded with a dot).

    In the first case accept other answers.

    But if you want the second solution, use: \b(?.

    Description:

    • \b - Start from a word boundary (but it allows the "after a dot" case).
    • (? - Negative lookbehind - exclude the "after a dot" case.
    • [a-z]+ - Match a sequence of letters.
    • (?=\.flower) - Positive lookahead for .flower.

    I assumed that you have only lower case letters, but if it is not the case, then add i (case insensitive) option.

    Another remark: Other answers include \w, which matches also digits and _ or even [^\.] - any char other than a dot (including e.g. \n).

    Are you happy with that? If you aren't, change to [a-z] (again, maybe with i option).

提交回复
热议问题