I'm on my phone now so I can't test, but it's a situation that occurs often in these type of patterns.jake2456 wrote: ↑September 6th, 2023, 2:08 am Ive got a small bug im trying to work out but im not sure how to fix it.
Im using this:To Capture this:Code: Select all
(?(?=.*"num":6,.*"home".*"runs":.*,.*"away".*"runs":.*,).*"num":6,.*"home".*"runs":(.*),.*"away".*"runs":(.*),)
But the "runs":0, section doesnt always exist for that inning until either a run is scored or the inning is over and they add another inning("num":7 section)Code: Select all
{"num":6,"ordinalNum":"6th","home":{"runs":0,"hits":0,"errors":0,"leftOnBase":0},"away":{"runs":0,"hits":2,"errors":0,"leftOnBase":0}}]
For example:So in turn it jumps ahead to the next instance:Code: Select all
{"num":6,"ordinalNum":"6th","home":{"hits":0,"errors":0,"leftOnBase":0},"away":{"runs":1,"hits":2,"errors":0,"leftOnBase":1}}],"teams":{"home":{"runs":1,"hits":3,"errors":0,"leftOnBase":3},"away":{"runs":1,"hits":8,"errors":0,"leftOnBase":7}},
which always exists and captures that instead those giving the wrong values.Code: Select all
"teams":{"home":{"runs":1,"hits":3,"errors":0,"leftOnBase":3},"away":{"runs":1,"hits":8,"errors":0,"leftOnBase":7}},
Ive tryd a few different ways. Even thought i had it solved with this:But for some reason this never captures a value for the away runs even if they exist.Code: Select all
(?(?=.*"num":6,"ordinalNum":".*","home":{"runs":.*,).*"num":6,"ordinalNum":".*","home":{"runs":(.*),)(?(?=.*"num":6,"ordinalNum":".*","home":{"runs":.*,"hits":.*,"errors":.*,"leftOnBase":.*},"away":{"runs":.*,).*"num":6,"ordinalNum":".*","home":{"runs":.*,"hits":.*,"errors":.*,"leftOnBase":.*},"away":{"runs":(.*),)
The solution for the condition should be simple, just an "OR aka | non capturing group" like (?:runs:.*,|) which is basically equivalent to an optional like (?:runs:.*,)?? where the second ? inverts the (?U) ungreedy initial flag back to greedy again to match the longer possible content first and make sure it's retrieved if it exists.
In the actual match part though, the solution is slightly complicated by the fact that you capture there, but even so, a "branch reset group" where the two alternative groups share the same group number should to the trick, like (?|runs:(.*),|()) where the second part, if it occurs, will have the same StringIndex but will capture the empty string.
By the way, if you don't like having nothing there you can replace the empty part in both the non capturing group and the branch reset one with .* since the flag is set to ungreedy anyway and * will match as few characters as possible aka zero.
See regexr.com where you set the regex flavor to the PCRE used by Rainmeter from the top right combo box and check Regex Reference > Groups & References sidebar to the left of the page.