arc000
arc000

Reputation: 873

Java regular expression consider empty values

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

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Make the second part optional.

^(?<key>.*?)(?:\\((?<value>.*)\\))?$

DEMO

Upvotes: 4

Related Questions