简体   繁体   English

字符串与正则表达式匹配

[英]String Matching with regular expressions

I am trying to write a regular expression in Python which takes a string and checks if: 我试图用Python编写一个正则表达式,该正则表达式接受一个字符串并检查是否:

  1. The last character is a vowel. 最后一个字符是元音。
  2. The last 2 characters are not the same. 后2个字符不同。

This is what I came up with: 这是我想出的:

[aeiou]$

Can anybody help me with point number 2: last 2 characters are not the same. 有人可以帮我指出第2点吗:最后2个字符不同。 For example, expresso is valid and expressoo is not valid. 例如, expresso有效,而expressoo无效。

It might be easier to do this without a regular expression. 如果没有正则表达式,执行此操作可能会更容易。

eg if s[-2]!=s[-1] and s[-1] in 'aeiou' 例如, if s[-2]!=s[-1] and s[-1] in 'aeiou'

(?i)([aeiouy])(?!\1)[aeiouy]$

EDIT: 编辑:

This is also appealing for not having a repeat: 这也很有吸引力,因为无需重复:

(?i)(?=[aeiouy]{2}$)(.)(?!\1).

我所能做的最好:

r"\w*(?:[^a\W]a|[^e\W]e|[^i\W]i|[^u\W]u|[^o\W]o)\b"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM