2

I have a string like that: obj[attr1=val1 attr2=val2 attr3=val3]
i need to extract object name and attributes.

Earlier, i've decided similar task in javascript using next regexp:

/^(\w+)(?:\[(\w+=\w+)(?:\s(\w+=\w+))*\])?$/

Now i have a trouble deciding in java:

Pattern pathPattern = Pattern.compile("^(\\w+)(?:\\[(\\w+=\\w+)(?:\\s+(\\w+=\\w+))*\\])?$");

I'm getting just a object name and first attribute. It seems that Mather class gets group count corresponding to count of "()" without considering symbol "*".

Is exists the possibility to make working java reg exp like js regexp, or i need to make two steps extraction?

thank you

3
  • Yes you are right. It does not consider everything matched by *. But just one group count. Commented Oct 23, 2012 at 6:33
  • I think it is strange way of realisation. I can use reg exp for matching but i can't use data. Commented Oct 23, 2012 at 6:42
  • When you use *, the pattern will be continuously replaced by the next match. Hence at last you would have only one matching string. That's why its only one group. Commented Oct 23, 2012 at 6:48

1 Answer 1

4

Matcher.groupCount() only counts the number of opening-brackets and consider them to be a group. So, the number of brackets you open will be the number of group counts (provided you are not using any non-capturing group).

You can use the below pattern to get the value inside the [.*]: -

Pattern pattern = Pattern.compile("(?:\\b)(\\w+?)=(\\w+?)(?:\\b)");
Matcher matcher = pattern.matcher(str);

while (matcher.find()) {
    System.out.println(matcher.group(1) + " : " + matcher.group(2));
}

This will match all the attr=val pair inside the [ and ].

OUTPUT: -

attr1 : val1
attr2 : val2
attr3 : val3

UPDATE: -

Since you don't have to do a boundary check in your above string, the above pattern can even be simplified to: -

Pattern pattern = Pattern.compile("(\\w+?)=(\\w+)");
Sign up to request clarification or add additional context in comments.

11 Comments

This way suppose two step decision. I'm interesting to make that in one step. Seems like in Java RegExp it not possible.
@BulatSafin. 2 step decision in the sense? I can't understand.
First: extract object name and brackets content. Second: parse brackets content.
@BulatSafin. That you have to do anyways. How do you want it to work?
@BulatSafin. Pattern Matching in Java regex works that way only. First you need to build a matcher object to match to the given string. and then perform the matching operation on that matcher.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.