Veronica Stromnoff
Veronica Stromnoff

Reputation: 51

Java Match string with optional hyphen

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

Upvotes: 5

Views: 3632

Answers (4)

anubhava
anubhava

Reputation: 784998

This regex should work for you:

^\w[\w-]*(?<=\w)\s*=\s*\"([^"]*)\"

In Java:

Pattern p = Pattern.compile("^\\w[\\w-]*(?<=\\w)\\s*=\\s*\"([^\"]*)\"");

Live Demo: http://www.rubular.com/r/0CvByDnj5H

Upvotes: 1

Bohemian
Bohemian

Reputation: 424993

Your problem is that you have the ? in the wrong place:

Try this regex:

^((\\w+-)?\\w+)\\s*=\\s*\"([^\"]+)\"

But use groups 1 and 3.

I've cleaned up the regex a bit too

Upvotes: 1

Pshemo
Pshemo

Reputation: 124215

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

Upvotes: 2

Patashu
Patashu

Reputation: 21773

You want something like this:

([\w\-]+)\s*=\s*"([^"]*)"

With extra backslashes for Java:

([\\w\\-]+)\\s*=\\s*\"([^\"]*)\"

If you expect other symbols to start appearing in the variable name, you could make it a character class like [^=\s] to accept any characters not = or whitespace, for example.

Upvotes: 0

Related Questions