I am trying to determine if a string contains at least one XML tag using the String.match()
function. Due to the way the project is set up, I would prefer if I didn't have to use a Pattern
.
Currently I use this Regex:
<[A-Za-z0-9]+>
Which obviously only checks if the string has right and left arrow brackets that contains text. What I need is a way to check if the string just has a single XML tag with Regex, eg input like:
blah <abc foo="bar">blah</abc> blah
blah <abc foo="bar"/>
but not input like:
blah <abc> blah
blah <abc </abc> blah
Is that possible?
This:
if (input.matches("(?s).*(<(\\w+)[^>]*>.*</\\2>|<(\\w+)[^>]*/>).*"))
matches both types of tag (standard and self-closing):
<abc foo="bar">blah</abc>
<abc foo="bar"/>
without matching incomplete tags like:
<abc>
See regex live demo.
You can use:
if (input.matches("(?s).*?<(\\S+?)[^>]*>.*?</\\1>.*")) {
// String has a XML tag
}
(?s)
is DOTALL
flag to make DOT match newlines also.
RegEx Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With