Reputation: 51
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
Reputation: 784998
This regex should work for you:
^\w[\w-]*(?<=\w)\s*=\s*\"([^"]*)\"
In Java:
Pattern p = Pattern.compile("^\\w[\\w-]*(?<=\\w)\\s*=\\s*\"([^\"]*)\"");
Upvotes: 1
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
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.
Upvotes: 2
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