java中两个分隔符之间的子字符串

时间:2016-11-03 15:09:12

标签: java android html extract

我有一个字符串:

<head>

    <script type="text/javascript">window._timings = {"domLoading": Date.now()}</script>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript">window._sharedData = {"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"};</script>
<script src="//instagramstatic-a.akamaihd.net/h1/bundles/en_US_ProfilePage.js/88ce62d41e70.js" type="text/javascript" crossorigin="anonymous"></script>

我只需要提取:

{"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"}

需要帮助。

1 个答案:

答案 0 :(得分:4)

就简单的字符串解析而言,您可以使用以下函数:

public static String between(String start, String end, String input) {
    int startIndex = input.indexOf(start);
    int endIndex = input.lastIndexOf(end);
    if(startIndex == -1 || endIndex == -1) return input;
    else return input.substring(startIndex + start.length(), endIndex + end.length()).trim();
}

在这种情况下,用法如下:

String input = "<head>\n" +
        "\n" +
        "    <script type=\"text/javascript\">window._timings = {\"domLoading\": Date.now()}</script>\n" +
        "\n" +
        "        <meta charset=\"utf-8\">\n" +
        "        <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
        "<script type=\"text/javascript\">window._sharedData = {\"country_code\": \"IR\", \"language_code\": \"en\", \"gatekeepers\": {\"cc\": true, \"sms\": true, \"ra\": true}, \"show_app_install\": false, \"static_root\": \"//instagramstatic-a.akamaihd.net/h1\", \"platform\": \"web\"};</script>\n" +
        "<script src=\"//instagramstatic-a.akamaihd.net/h1/bundles/en_US_ProfilePage.js/88ce62d41e70.js\" type=\"text/javascript\" crossorigin=\"anonymous\"></script>";
System.out.println(between("window._sharedData =", "}", input));

结果符合预期:

{"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"}