Extract existing value between two tags with regular expression

-1

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

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

    
asked by anonymous 17.03.2016 / 19:53

1 answer

2

First

  

HTML is not a regular language and therefore can not be rendered by a regular expression.

You should use an appropriate tool for these cases.

However

If you have a "regular" interval in which you are sure to 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));

See on Ideone

More about REGEX in Java

    
18.03.2016 / 13:05