简体   繁体   中英

PHP Regex match if last three characters contain two letters

Basically I have this but its either or:

[en].{0,2}$

What I want to do is detect 'en' and not 'e' or 'n'. Just 'en' in that order but in the last three characters.

So it should match

attend

but not:

attained

So it must detect if the last three characters contain 'en' in order.

Just 'en' in that order but in the last three characters.

That means you need to match en that is either at the end of the string or en that has any single char after it at the end of the string.

You may use

en.?$

See the regex demo

Details

  • en - en substring
  • .? - any optional char
  • $ - end of string.

If you may have line breaks in the input, use s modifier: /en.?$/s .

如果bean也可以使用这个正则表达式:

/e.?n$|en.?$/g

[Regex Demo]

you can use the regex

^.*(?=.*en.*).{3}$

see the regex 101 demo

If you want the last three characters starts with en, You can use this regex

en.$

If you want the last three characters ends with en, You can use this regex

.en$

If both cases are acceptable, You can use this regex

(.en|en.)$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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