getBetween特定页面上的两个字符串?

时间:2012-03-21 01:12:54

标签: java string methods

我目前有一个像这样的getBetween函数:

public static String getBetween(String haystack, String pre, String post) {
    Pattern pattern = Pattern.compile(pre+"(.+?)"+post);
    Matcher matcher = pattern.matcher(haystack);
    if(matcher.find())
        return haystack.substring(matcher.start(1),matcher.end(1));
    return "No match could be found.";
}

当我尝试获取两个字符串中的字符串时,它就不起作用。

像:

System.out.println(getBetween(strHTML, "href="/objects.phtml?type=inventory">", "</a> <span style="font-weight: normal;">"));

我有什么方法可以解析这两个字符串,以便它可以使用“s并且仍可以使用getBetween()吗?

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你想要打印出strHTML中间的某个地方吗?问题是你的prepost得到了编译,我认为html与Java Pattern中的含义不同。所以你需要引用它们。

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


public class GetBetween
{

    public static String getBetween(String haystack, String pre, String post)
    {
        Pattern pattern = Pattern.compile( Pattern.quote(pre) + "(.+?)" + Pattern.quote(post));
        Matcher matcher = pattern.matcher(haystack);

        if (matcher.find())
        {
            return haystack.substring(matcher.start(1),matcher.end(1));
        }

        return "No match could be found.";
    }


    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String strHTML = "href=\"/objects.phtml?type=inventory\">\"s</a><span style=\"font-weight: normal;\">";

        System.out.println(getBetween(strHTML, "href=\"/objects.phtml?type=inventory\">", "</a><span style=\"font-weight: normal;\">"));

       strHTML = "href=\"/objects.phtml?type=inventory\"></a><span style=\"font-weight: normal;\">";

        System.out.println(getBetween(strHTML, "href=\"/objects.phtml?type=inventory\">", "</a><span style=\"font-weight: normal;\">"));
    }

}

答案 1 :(得分:0)

尝试这样的事情:

int startIndex = haystack.indexOf(pre);
// handle the case where startIndex is -1
int endIndex = haystack.indexOf(startIndex, post);
// handle the case where endIndex is -1
return haystack.substring(startIndex, endIndex)