What regex do I use to select the first instance of a string in single quotes when the line contains multiple comma separated single quote values ?
There could be multiple lines and I want to match first instance on each line.
‘(.*?)’ selects all instances so the lazy quantified is not working exactly as I expected
Here’s an example of what I’m seeing.
CodePudding user response:
Remove the global modifer. Click on Flags and remove global, so it's /'(.*?)'/ instead of /'(.*?)'/g
CodePudding user response:
You can use
^.*?'\K.*?(?=')
See the regex demo.
Details:
^- start of string.*?- zero or more chars other than line break chars as few as possible'- a'char\K- match reset operator that discards all text in the match memory buffer.*?- zero or more chars other than line break chars as few as possible(?=')- a positive lookahead that requires a'to appear immediately to the right of the current location
Probably a bit more efficient variation of the same regex will be
^[^'\n\r]*'\K[^'\n\r]*(?=')
where [^'\n\r] finds any char other than ' and LF/CR chars.
