用于匹配字符串的正则表达式,如果该字符串以特定字符结尾,则不匹配

时间:2018-06-04 16:34:46

标签: python regex

我在尝试制作正则表达式以匹配字符串ka toki.*(例如ka tokika toki knfjk)时遇到问题,但如果{{1}之后的第一个符号不匹配是} toki

换句话说,我正试图匹配这些:

a

但不是这些:

ka toki
ka toki sadbkhbhd habsd
ka tokijhvkhsd

2 个答案:

答案 0 :(得分:1)

Use alternation to select both the case where no characters follow and the case where a character other than A follows:

ka toki($|[^A].*)

See https://www.regular-expressions.info/alternation.html

答案 1 :(得分:1)

To match a whole string starting with ka toki that is not followed with a, you may use

re.match(r'ka toki(?!a).*$', s)

Python's re.match anchors the match at the start of the string, thus, you may omit the ^ anchor at the start of the ^ka toki(?!a).*$ pattern.

See the regex demo.

Pattern details

  • ^ - start of the string (implicit in re.match)
  • ka toki - a literal substring
  • (?!a) - a negative lookahead that fails the match if the a char follows the ka toki substring
  • .* - the rest of the line, 0+ chars other than a newline (you may yse re.DOTALL or re.S to make it match across lines)
  • $ - end of the string (actually, it can be omitted if you expect only single line matches).

Note that the same can be achieved without a lookaround, by using a negated character class within an optional group:

re.match(r'ka toki(?:[^a].*)?$', s)

See another regex demo.

Here, (?:[^a].*)? is an optional non-capturing group that matches 1 or 0 occurrences of any char but a (with [^a]) and then .* matches the rest of the string.