Assume a one-line string with multiple consecutive key-value pairs, separated by a space, but with space allowed also within values (not in keys), e.g.
key1=one two three key2=four key3=five six key4=seven eight nine ten
Correctly extracting the key-value pairs from above would produce the following mappings:
"key1", "one two"
"key2", "four"
"key3", "five six"
"key4", "seven eight nine ten"
where "keyX" can be any sequence of characters, excluding space.
Trying something simple, like
([^=]+=[^=]+)+
or similar variations is not adequate.
Is there a regex to fully handle such extraction, without any further string processing?
\1
contains the key and \2
the value:
(key\d+)=(.*?)(?= key\d+|$)
Escape \
with \\
in Java:
(key\\d+)=(.*?)(?= key\\d+|$)
Demo: https://regex101.com/r/dO8kM2/1
Try with a lookahead:
(\b\w+)=(.*?(?=\s\w+=|$))
As a Java String:
"(\\b\\w+)=(.*?(?=\\s\\w+=|$))"
Test at regex101.com; Test at regexplanet (click on "Java")
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