I need to separate (group) the components from lines of text that are essentially composed of a key and value.
The first whole word is the key, followed by an arbitrary amount of white space, then everything else after that white space ends is the value, including more whitespace, but ideally not trailing whitespace.
The key can also have leading whitespace.
Additionally, it is possible for the value to be null, in which case the string should still match with a single capture group for the key
Example
Key: "key"
Value: "I am a value"
Test Cases (underscores represent spaces):
key
key___
___key___
key___I_am_a_value
__key___I_am_a_value
__key______I_am_a_value_______
In all of these situations I'd like to end up with two capture groups, each containing the key and value as they are shown between the quotes above, with the second group being null when a value is not present
To clarify, in this situation I'm using whitespace to refer to spaces and tabs, but not line breaks.
This seems pretty close, except that it still includes trailing whitespace in the value and I'm not sure how to drop that:
(?<key>\w+)(?:[ \t]*(?<value>.*))
As a final example to highlight this issue, with the above and this test string (again '_' = ' '):
____people_________john_jim_jen_josh____
I'm getting
key: "people"
value: "john jim jen josh "
when I want:
key: "people"
value: "john jim jen josh"