Reputation: 7043
From this possible input i want to mach the bold text:
Genre: Thriller, Adventure, Action, 2012
Genre: Thriller, Adventure, Action
I did something like this (?<=Genre: ).*(?=(, \d{4})?)
My problem is that I put this part (, \d{4})?
hoping that because there is match ", 2012"
it would stop at this point but it not, its just ignore the matching...
Any advice?
Upvotes: 0
Views: 342
Reputation: 12785
.*
is greedy, so it will consume anything it can. Since the last part with the digits is optional, it all gets consumed by the .*
.
There may be a more eloquent way of doing it, but maybe just have two regexes:
(?<=Genre: ).*(?=(, \d{4}))|(?<=Genre: ).*
If I'm right the first will match in the case with the numbers, but it will fall through to the more general case if that fails.
Edit: Actually, it might work to do something like the following:
(?<=Genre: ).*?(?=(, \d{4})|$)
That way the .*
is not greedy, but it must consume everything up to either the digits or else the end of the string.
Upvotes: 2