user1275154
user1275154

Reputation: 1150

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

a!!1

a!1!a!!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

Thanks, Stevo

Upvotes: 0

Views: 906

Answers (2)

h0x0
h0x0

Reputation: 495

(.*)\!(.*)

Matched test inputs/outputs here http://www.myregextester.com/index.php

Upvotes: 2

1010
1010

Reputation: 1848

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

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

Upvotes: 0

Related Questions