I am trying to create a regex expression that would match one or multiple variable value assignments on the same line. I am using the following expression:
([a-z][a-zA-Z0-9-]*)=(('(\'|[^\'])*')|("(\"|[^"])*"))
For example, if I have the following string as input:
a="xyz" b="hello world"
And using the following code:
Matcher matcher = rules.get(regex).matcher(input);
int start = 0;
while (matcher.find(start)) {
System.err.println(matcher.group(0));
start = matcher.end();
}
It should give me two seperate results:
1. a="xyz"
2. b="hello world"
But it only returns one, the entire input string.
a="xyz" b="hello world"
It seems to be taking xyz" b="hello world as the inner part. How can I resolve this?
You may use
(?s)([a-z][a-zA-Z0-9-]*)=(?:'([^\\']*(?:\\.[^\\']*)*)'|"([^"\\]*(?:\\.[^"\\]*)*)")
See the regex demo
In Java,
String regex = "(?s)([a-z][a-zA-Z0-9-]*)=(?:'([^\\\\']*(?:\\\\.[^\\\\']*)*)'|\"([^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\")";
Details
(?s) - inline Pattern.DOTALL embedded flag option that matches . match line break chars, too([a-z][a-zA-Z0-9-]*) - Group 1= - an equals sign (?:'([^\\']*(?:\\.[^\\']*)*)'|"([^"\\]*(?:\\.[^"\\]*)*)") - a non-capturing group matching one of the two alternatives:
'([^\\']*(?:\\.[^\\']*)*)' - ', then any amount of chars other than \ and ' followed with 0+ repetitions of any escape sequence followed with 0+ chars other than \ and ' | - or "([^"\\]*(?:\\.[^"\\]*)*)" - ", then any amount of chars other than \ and " followed with 0+ repetitions of any escape sequence followed with 0+ chars other than \ and " .If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With