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
2009-08-06 11:25:10
Isn't the wild card for end-of-string missing in this expression?
0xA3
2009-08-06 11:28:41
@divo: no, the *substring match* should end on `xyz`, not the whole string.
Konrad Rudolph
2009-08-06 11:29:45
Ah, alright, I got confused by the heading.
0xA3
2009-08-06 11:34:33