Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Match string with optional hyphen

Tags:

java

regex

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

like image 938
Veronica Stromnoff Avatar asked Oct 05 '22 01:10

Veronica Stromnoff


1 Answers

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

like image 134
Pshemo Avatar answered Oct 13 '22 11:10

Pshemo