I am trying to match a series of string thats looks like this:
item1 = "some value"
item2 = "some value"
I have some strings, though, that look like this:
item-one = "some new value"
item-two = "some new value"
I am trying to parse it using regular expressions, but I can't get it to match the optional hyphen.
Here is my regex string:
Pattern p = Pattern.compile("^(\\w+[-]?)\\w+?\\s+=\\s+\"(.*)\"");
Matcher m = p.matcher(line);
m.find();
String option = m.group(1);
String value = m.group(2);
May someone please tell me what I could be doing wrong. Thank you
I suspect that main reason of your problem is that you are expecting w+?
to make w+
optional, where in reality it will make +
quantifier reluctant so regex will still try to find at least one or more \\w
here, consuming last character from ^(\\w+
.
Maybe try this way
Pattern.compile("^(\\w+(?:-\\w+)?)\\s+=\\s+\"(.*?)\"");
in (\\w+(?:-\\w+)?)
-> (?:-\\w+)
part will create non-capturing group (regex wont count it as group so (.*?)
will be group(2) even if this part will exist) and ?
after it will make this part optional.
in \"(.*?)\"
*?
is reluctant quantifier which will make regex to look for minimal match that exist between quotation marks.
Demo
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