我如何正则表达式匹配(非包容性)?

时间:2022-12-01 16:45:23

I want to get the String between (not including): alt=" and " Here is a small sample of my code:

我想得到(不包括)之间的字符串:alt =“和”这是我的代码的一小部分示例:

Pattern p2 = compile("alt=\"(.*?)\");
Matcher m2 = p2.matcher(result);
while (m2.find()) {
    names.add(m2.group());
}

The output is for example: alt="Harry Potter"
when I want the output to be just: Harry Potter

输出是例如:alt =“哈利波特”当我想要输出只是:哈利波特

1 个解决方案

#1


1  

Your code has a typo (a missing double quote in compile) and the group you need to access is Group 1 (use compile("alt=\"(.*?)\"") and m2.group(1)).

您的代码有拼写错误(编译时缺少双引号),您需要访问的组是第1组(使用compile(“alt = \”(。*?)\“”)和m2.group(1))。

You should think about using an HTML parser for getting values from HTML, like jsoup. Here is a way to get what you need with it:

您应该考虑使用HTML解析器从HTML获取值,例如jsoup。这是一种通过它获得所需内容的方法:

Document doc = Jsoup.parse(html_contents);
for (Element element : doc.getAllElements())
{
    for (Attribute attribute : element.attributes())
    {
        if(attribute.getKey().equalsIgnoreCase("alt"))
        {
            names.add(attribute.getValue());
        }
    }
}

#1


1  

Your code has a typo (a missing double quote in compile) and the group you need to access is Group 1 (use compile("alt=\"(.*?)\"") and m2.group(1)).

您的代码有拼写错误(编译时缺少双引号),您需要访问的组是第1组(使用compile(“alt = \”(。*?)\“”)和m2.group(1))。

You should think about using an HTML parser for getting values from HTML, like jsoup. Here is a way to get what you need with it:

您应该考虑使用HTML解析器从HTML获取值,例如jsoup。这是一种通过它获得所需内容的方法:

Document doc = Jsoup.parse(html_contents);
for (Element element : doc.getAllElements())
{
    for (Attribute attribute : element.attributes())
    {
        if(attribute.getKey().equalsIgnoreCase("alt"))
        {
            names.add(attribute.getValue());
        }
    }
}