日期:2014-05-20  浏览次数:20856 次

JAVA正则表达式的字符串查找操作
在C#中有如下操作:

C# code
public static string[] GetRegValue(string HtmlCode, string RegexString, string GroupKey)
    {
        MatchCollection m;
        Regex r;
        r = new Regex(RegexString, RegexOptions.Multiline | RegexOptions.Singleline);
        m = r.Matches(HtmlCode);
        string[] MatchValue = new string[m.Count];
        for (int i = 0; i < m.Count; i++)
        {
            MatchValue[i] = m[i].Groups[GroupKey].Value;
        }
        return MatchValue;
    }


以上方法可进行复杂的字符串提取功能,如:
C# code
GetRegValue(text, "name=\"mailAddress\" type=\"text\" value=\"(?<id>.*?)\"", "id")[0];

可以将文本中一串 ...name="mailAddress" type="text" value="要提取的内容"...严格的提取出来,而不用管要提取的目标串的格式。
请问在JAVA中如何使用正则实现以上功能(在字符串中查找字符串)?

------解决方案--------------------
请问是否只提取备案信息?是否备案链接地址总是包含“miibeian”?
下面给出只提取备案信息的代码
Java code

    Pattern p = Pattern.compile("miibeian.*?>(.*?)<");
    Matcher m = p.matcher(" <a href=http://www.miibeian.gov.cn target=_blank>京ICP证030173号</a>");
    if(m.find()){
        System.out.println(m.group(1));
    }