I want to match following pattern:
key="value" key="value" key="value" key="value" ...
where key and value are [a-z0-9]+, both should be grouped (2 groups, the ” – chars can be matched or skipped)
input that should not be matched:
key=”value”key=”value” (no space between pairs)
For now I got this(not .NET syntax):
([a-z0-9]+)=(\"[a-z0-9]+\")(?=\s|$)
the problem with that, that it matches key4=”value4″ in input:
key3="value3"key4="value4"
The spec isn’t very clear, but you can try:
Or, as a C# string literal:
This uses a negative lookarounds to ensure that the the key-value pair is neither preceded nor followed by non-whitespace characters.
Here’s an example snippet (as seen on ideone.com):
Related questions
(?<=#)[^#]+(?=#)work?On validating the entire input
You can use
Regex.IsMatchto see if the input string matches against what should be the correct input pattern. You can also use the same pattern to extract the keys/values, thanks to the fact that .NET regex lets you access individual captures.The above prints (as seen on ideone.com):
References
Explanation of the pattern
The pattern to validate the entire input is essentially:
Where each entry is:
That is, a key/value pair followed by either spaces or the end of the string.