Extract existing value between two regular expression tags

Asked

Viewed 709 times

-1

How to check from a regular expression whether there is a certain value between two strings, for example, tags <code> and </code>?

I want, for example, to know if there is between the two tags the value "03". How to write an expression that meets this need?

  • 3

    I’m not familiar with java, but isn’t searching for an xml parser better than regex? http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

1 answer

2


First of all

HTML is not a regular language and therefore cannot be processed by a regular expression.

An appropriate tool should be used for these cases.

However

If you have a "regular" range in which you are sure you will always follow the same pattern, you can use REGEX.

String s = "<div style=\"border:1px solid #CCC\">03</div>";
String t = "div";
String p = "<("+t+").*>([^<]*?)</\\1>";
Pattern r = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
Matcher m = r.matcher(s);
m.find();

System.out.print(m.group(2));

Check it out at Ideone

More about Java REGEX

Browser other questions tagged

You are not signed in. Login or sign up in order to post.