基本上我有这个,但它要么:
[en].{0,2}$
我想要做的是检测“en”而不是“e”或“n”。只是按顺序“en”,但在最后三个字符中。
所以应该匹配
attend
但不是:
attained
所以它必须检测最后三个字符是否按顺序包含'en'。
最佳答案
Just 'en' in that order but in the last three characters.
这意味着您需要匹配字符串末尾的 en
或字符串末尾后面有任何单个字符的 en
。
你可以使用
en.?$
参见 regex demo
详情
en
-en
子串.?
- 任何可选的字符$
- 字符串结尾。
如果输入中可能有换行符,请使用 s
修饰符:/en.?$/s
。
关于如果最后三个字符包含两个字母,则 PHP 正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45907852/