Java regular expression to retrieve data between tags

I am trying to use a regular expression that outputs data from a string like

<B Att="text">Test</B><C>Test1</C>

The output selected should be Test and Test1. This is what I have done so far:

public class HelloWorld {
    public static void main(String[] args)
    {
        String s = "<B>Test</B>";
        String reg = "<.*?>(.*)<\\/.*?>";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(s);
        while(m.find())
        {
            String s1 = m.group();
            System.out.println(s1);
        }
    }
}

But this leads to a result <B>Test</B>. Can someone point out what I'm doing wrong?

+3
source share
4 answers

Three problems:

  • Your test string is incorrect.
  • You need an inanimate modifier in a group.
  • You need to specify which group you want (group 1).

Try the following:

String s = "<B Att=\"text\">Test</B><C>Test1</C>"; // <-- Fix 1
String reg = "<.*?>(.*?)</.*?>";                   // <-- Fix 2
// ...
String s1 = m.group(1);                            // <-- Fix 3

You also don't need to avoid the slash, so I removed this.

See how it works on ideone .

( , HTML - HTML-.)

+7

eclipse, , , , . : http://regex-util.sourceforge.net/update/ , "" → " " → "", " "

,

+2

, XML / HTML. .

+1

, XML, XML.

regex try:

<B[^>]*>(.+?)</B\s*>

, B.

, , - :

<.*?>(.*?)</.*?>
+1

Source: https://habr.com/ru/post/1764918/


All Articles