简体   繁体   English

正则表达式匹配所有字符,但最后一次出现

[英]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 一个!1

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

a!!1 一个!! 1

  • G1: a! G1:一个!
  • G2: 1 G2:1

a!1!a!!a 一个!1!一!一

  • G1: a!1!a! G1:a!1!a!
  • G2: 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 不使用RightToLeft

Thanks, Stevo 谢谢,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 在一个捕获组中,之后的所有内容在另一个捕获组中

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

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

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