如何对与模式匹配的正则表达式的重复出现进行分组

时间:2016-10-20 05:53:50

标签: java regex

我在java中有以下字符串

59701=Application+Development"">app dev</a>| <a href=""/search/search.asp?cdlCustomFieldValue001=Data+Analytics+(formerly+Business+Intelligence)"">Data Analytics (formerly Business Intelligence)</a>| <a href=""/search/search.asp?cdlCustomFieldValueID012=Small+and+Midmarket+Cloud+Solutions"">Small and Midmarket Cloud Solutions</a></td></tr><tr class=""CstmFldRow noVpad CfProfessional"" style=""""><td valign=""top""><label class=""CstmFldLbl"">

我需要应用正则表达式,以便我只需要提取文本 app dev,Data Analytics(以前的商业智能)和 中型市场云解决方案。

我使用了以下正则表达式

        String text_string= "59701=Application+Development"">app dev</a>| <a href=""/search/search.asp?cdlCustomFieldValue001=Data+Analytics+(formerly+Business+Intelligence)"">Data Analytics (formerly Business Intelligence)</a>| <a href=""/search/search.asp?cdlCustomFieldValueID012=Small+and+Midmarket+Cloud+Solutions"">Small and Midmarket Cloud Solutions</a></td></tr><tr class=""CstmFldRow noVpad CfProfessional"" style=""""><td valign=""top""><label class=""CstmFldLbl"">";

        Pattern pattern_sub51 = Pattern.compile("\"\">(.*?)</a>");
        Matcher matchersub51 = pattern_sub51.matcher(text_string);
                 if(matchersub51.find())
                 {
                     store_variable=matchersub51.group(1).trim();

                 }

现在它只提取app dev文本。但是,为了找到其他文本值,我需要使用模式并重复匹配。有没有办法这样做以递归方式查找值并将它们存储在java变量中?

1 个答案:

答案 0 :(得分:0)

是@wiktor Stribizew的建议,以下代码有效。感谢

import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String text_string= "59701=Application+Development\"\">app dev</a>| <a href=\"\"/search/search.asp?cdlCustomFieldValue001=Data+Analytics+(formerly+Business+Intelligence)\"\">Data Analytics (formerly Business Intelligence)</a>| <a href=\"\"/search/search.asp?cdlCustomFieldValueID012=Small+and+Midmarket+Cloud+Solutions\"\">Small and Midmarket Cloud Solutions</a></td></tr><tr class=\"\"CstmFldRow noVpad CfProfessional\"\" style=\"\"\"\"><td valign=\"\"top\"\"><label class=\"\"CstmFldLbl\"\">";
        Pattern pattern_sub51 = Pattern.compile("\"\">(.*?)</a>");
        Matcher matchersub51 = pattern_sub51.matcher(text_string);
        List<String> res = new ArrayList<>();
        while(matchersub51.find())
        {
            res.add(matchersub51.group(1).trim());
        }
        System.out.println(res);
    }
}