简体   繁体   中英

Regex: find last occurrence of pattern

I am trying to find a regular expression that will match the last occurrence of a specific language tag with no other language tags found afterwards.

  • specific language tag: _en
  • language tag: _[az]{2}

For example, the pattern should match all of the following:

title_en
components_en.video.title
components_en.video.title_en
components_en.video.the_end
components_es.video.title_en

Also, the pattern should not match any of the following:

title_es
components_es.video.title
components_en.video.title_es
components_es.video.the_end

This should do it:

_en(\.|$)(?!.*_[a-z]{2}(\.|$))

Here's a fiddle showing how it performs on your test cases.

If you want to match the entire string, prefix the expression with ^.* and suffix with .*$ .

^.*_en(\.|$)(?!.*_[a-z]{2}(\.|$)).*$

Results are shown in this fiddle .

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