tags:

views:

67

answers:

1

I need a regex that matches first "xyz" and all characters that come before. For example, for "abxyzcdxyz" it should match "abxyz". I was trying with pattern ".*xyz", but it matches the entire string.

+5  A: 

Try non-greedy matching:

.*?xyz

*? is a non-greedy quantifier, i.e. it matches zero or more occurrences, but as few as possible.

Konrad Rudolph
Isn't the wild card for end-of-string missing in this expression?
0xA3
@divo: no, the *substring match* should end on `xyz`, not the whole string.
Konrad Rudolph
Ah, alright, I got confused by the heading.
0xA3