I have this regex that is supposed to match 1=’aa’ or 1=”aa” or 1=aa, and return number/value.
(?<number>\d)=\s*("|')?\s*(?<value>.*?)(?=("|')?\d=|$)
it works but it is returning the value incorrectly. Number comes back as 1 but value as aa” in the case of 1=”aa”
How can I get value = aa for the case of 1=”aa”.
Actual expression may contain 1=’aa’ 2=”bb” 3=cc etc.
The main problem here is that this:
means “followed by any of the following:
"plus a digit plus='plus a digit plus==“. You’ll notice that it does not make any allowance for whitespace between the
"or'and the digit; so in the case of1="aa" 2=..., thevaluesimply is not allowed to be followed by" 2=.... Similarly, it does not make any allowance for"or'plus end-of-string.So the minimal fix is to allow — nay, require — some whitespace before the digit, and to move the
("|')?out of the lookahead assertion and into the main part of the regex:giving:
While we’re at it, we might as well make some other tweaks to simplify the regex and reduce the number of cases where it can go wrong:
(Further cleanup may be possible, but I don’t know enough about your data to recommend any more changes.)