Python regex to match words not having dot

2020-05-08 00:03发布

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, "pink.blue.flower" is unacceptable. Can anyone help how to do this in python using regex?

标签: python regex
5条回答
一夜七次
2楼-- · 2020-05-08 00:51

To match any character except a newline or a dot you could use a negated character class [^.\r\n]+ and repeat that one or more times and use anchors to assert the start ^ and the end $ of the line.

^[^.\r\n]+\.flower$

Or you could specify in a character class which characters you would allow to match followed by a dot \. and flower.

^[a-z0-9]+\.flower$

查看更多
▲ chillily
3楼-- · 2020-05-08 00:52

You are looking for "^\w+\.flower$".

查看更多
乱世女痞
4楼-- · 2020-05-08 00:53

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(?<!\.)[a-z]+(?=\.flower).

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).

查看更多
Bombasti
5楼-- · 2020-05-08 00:57

Here is the regex for you. ^([^\.]*)\.flower$. Example: https://regex101.com/r/cSL445/1.

查看更多
啃猪蹄的小仙女
6楼-- · 2020-05-08 01:00

Is this sufficient?

^\w+\.\w+$
查看更多
登录 后发表回答