如何使用正则表达式提取TAG内容

时间:2019-02-12 08:23:10

标签: java regex regex-group


这是我的Regex模式,用于在html标签之间提取数据。

(<.*?>)(.*?)(<\/.*?>)

它涵盖了大多数要求。
这是我的正则表达式示例link

我要解决两个问题。  
01.在第二个示例中,我无法捕获第二个<h1>标签。
02.在第三个示例中,正则表达式标签不同。 regex result image 请帮忙。
谢谢。


已编辑:这是整个示例

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Hello{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int testCases = Integer.parseInt(scan.nextLine());

        while (testCases-- > 0) {
            String line = scan.nextLine();

            boolean matchFound = false;
           Pattern r = Pattern.compile("(<.*?>)(.*?)(<\\/.*?>)");
            Matcher m = r.matcher(line);

            while (m.find()) {
                System.out.println(m.group(2));
                matchFound = true;
            }
            if ( ! matchFound) {
                System.out.println("None");
            }
        }
    }
}

这是我想要的输出。

Nayeem loves counseling
Sanjay has no watch
So wait for a while
None
Imtiaz has a secret crush

0 个答案:

没有答案