Reputation: 12044
My text is:
120 something 130 somethingElse Paris
My goal is to capture 130 somethingElse Paris
which means only the last occurrence of number BEFORE Paris
I tried:
\d+.*Paris
But this captures the WHOLE string (from first occurrence of digit)
The rule is:
Paris
until first occurrence of digit is found.Any clue ?
regards
Upvotes: 0
Views: 1118
Reputation: 89557
You can use this pattern:
(\d+\D*?)+Paris
other occurences of the capturing group are overwritten by the last.
The lazy quantifier *?
is used to force the pattern to stop at the first word "Paris". Otherwise, in a string with more than one word "Paris", the pattern will return the last group after the last word "Paris" with a greedy quantifier.
Upvotes: 0
Reputation: 28403
for last occurrence
^code:[ ]([0-9a-f-]+)(?:(?!^code:[ ])[\s\S])*Paris
you have to customize with your text.
Please refer this:
Regex match everything from the last occurrence of either keyword
Match from last occurrence using regex in perl
RegExp: Last occurence of pattern that occurs before another pattern
Regex get last occurrence of the pattern
Upvotes: 1
Reputation: 5340
Try this regex:
/(\d+[^\d]*Paris)/gi
http://jsfiddle.net/rooseve/XDgxL/
Upvotes: 1
Reputation: 13725
You should add a ? after the * to make it un-greedy. Like this:
\d+.*?Paris
Upvotes: 0