Reputation: 873
I have some key value pairs as - key(value)
example data -
name(alex)
crimehistory
address(newland)
I am processing the data line by line and want to fetch the key value pairs. If value is not present it should be null (crimehistory).
My current regular expression is -
String pattern = "^(?<key>.*?)\\((?<value>.*)\\)";
But it gets me the key value only for name and address. What should I modify in the regular expression so that it gets me the key 'crimehistory' with null value ?
Upvotes: 0
Views: 85
Reputation: 174706
Make the second part optional.
^(?<key>.*?)(?:\\((?<value>.*)\\))?$
Upvotes: 4