0
<InstanceIdentifier>118e3040-51d2-11e3-8f96-1110200c9a66</InstanceIdentifier>

This is my string, and i want the 118e3040-51d2-11e3-8f96-1110200c9a66 part.

tried this

Pattern p = Pattern.compile("(\\b<InstanceIdentifier>\\b)(.*?)(\\b<\/InstanceIdentifier>\\b)");

but of course it's not working. Suggestions?

1
  • Regex can be simplified to "<InstanceIdentifier>(.*?)</InstanceIdentifier>" Commented Aug 1, 2015 at 16:35

1 Answer 1

1

Just remove the first and last \\b then remove the backslash which exists before /

Pattern p = Pattern.compile("(<InstanceIdentifier>\\b)(.*?)(\\b</InstanceIdentifier>)");

or

Pattern p = Pattern.compile("<InstanceIdentifier>\\b(.*?)\\b</InstanceIdentifier>");
Matcher m = p.matcher(s);
if(m.find())
{
System.out.println(m.group(1));
}

Note that \\b matches between a word character and a non-word character, so the above regex must except a word character next to the starting INstanceIdentifier tag and before the closing INstanceIdentifier tag.

Your regex fails because there isn't a word character character which actually exists between the start of the line and the opening < bracket , likewise there isn't a word character which exists next to > and end of the line boundary. In this case, adding \\B instead of \\b at the start and end should work.

Sign up to request clarification or add additional context in comments.

Comments

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.