I have multiple lines as follows where 'ProgressiveAwardWinnings' portion may or may not exist. I am trying to extract the number data from these lines.
LineStake":5000.0,"Won":200, -something- ProgressiveAwardWinnings":10000 LineStake":5000.0,"Won":100, -something-
Thus the extracted information from first line will be 5000.0, 200, 10000 and second line will be 5000.0, 200, null.
My regex is as follows. The 3rd value shows as null for both lines. I am expecting the 3rd value to be captured for line 1. Please advice.
Regex
LineStake\":([0-9\.]+),\"Won\":([0-9\.]+),.*?(?:ProgressiveAwardWinnings\"\:([0-9]+))?
答案 0 :(得分:2)
The problem is that the lazy .*?
matches 0+ chars as few as possible, and is skipped the first time the regex engine comes up to that pattern part, and tries to match the rest of the patterns. If they match, the .*?
is not even tried, is not expanded. The (?:...)?
non-capturing group is optional, it can match an empty string.
So, once 200
is matched on line 1, .*?
is skipped and (?:...)?
matches an empty space after 200
, and the match is returned at that point.
The solution is to put .*?
into the optional group:
LineStake\":([0-9\.]+),\"Won\":([0-9\.]+),(?:.*?ProgressiveAwardWinnings\"\:([0-9]+))?
^^^
See the regex demo