简体   繁体   中英

Regex matching all but last occurence of character

I'm trying to put together a Regex matching all but last occurrence of a specific character (say exclamation mark ! ), into a group, and then everything else into another group.

Examples:

a!1

  • G1: a
  • G2: 1

a!!1

  • G1: a!
  • G2: 1

a!1!a!!a

  • G1: a!1!a!
  • G2: a

It is safe to assume that the special character cannot be at the start, or the end of the string.

Ideally I want all this to be achieved in

  • one Regex
  • no additional manipulation with strings
  • without using RightToLeft

Thanks, Stevo

(.*)\!(.*)

匹配的测试输入/输出在这里http://www.myregextester.com/index.php

this should catch anything before the last ! in a capturing group, and everything after in the other capturing group

(.*)!([^!]*)

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