Brad
Brad

Reputation: 6332

Match all characters up to a capture group, excluding the capture group

I have a pattern

blah_blah_blah_blah_19292_blah_m32.jpg

I want to match everything up to _mXX while excluding the _mXX.

I have ^(.*[_m]) but this is inclusive. Can't seem to get the capture group excluded. What am I missing?

rubular link http://www.rubular.com/r/0Ls12Z6GH7

Upvotes: 1

Views: 96

Answers (1)

anubhava
anubhava

Reputation: 784958

You can use positive lookahead for that:

/^.*?(?=_m[0-9]+)/

Will match blah_blah_blah_blah_19292_blah in your input string.

Online Demo: http://www.rubular.com/r/abgW0Q1gjX

Referece: Lookarounds in regex

Quoting from above link:

q(?=u) matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.

Upvotes: 2

Related Questions