Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex check if string contains XML tag

Tags:

java

regex

xml

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?

like image 588
Toby Caulk Avatar asked Dec 25 '22 17:12

Toby Caulk


2 Answers

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.

like image 152
Bohemian Avatar answered Jan 05 '23 19:01

Bohemian


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

like image 42
anubhava Avatar answered Jan 05 '23 20:01

anubhava