简体   繁体   English

字符串中的Java正则表达式

[英]Java regex from string

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

This is my string, and i want the 118e3040-51d2-11e3-8f96-1110200c9a66 part. 这是我的字符串,我想要118e3040-51d2-11e3-8f96-1110200c9a66部分。

tried this 试试这个

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

but of course it's not working. 但是当然不行。 Suggestions? 有什么建议吗?

Just remove the first and last \\\\b then remove the backslash which exists before / 只需删除第一个和最后一个\\\\b然后删除/之前的反斜杠即可

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. 请注意, \\\\b在单词字符和非单词字符之间匹配,因此上述正则表达式必须除起始INstanceIdentifier标记旁边和结束INstanceIdentifier标记之前的单词字符外。

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. 在这种情况下,应该在开头和结尾添加\\\\B而不是\\\\b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM