正则表达式android

时间:2012-08-02 14:56:54

标签: android regex

我有像这样的字符串,例如

309 \ 306 \ 308 \ 337_338

309 \ 306 \ 337_338

310

311 \ 315_316 \ 336_337

311 \ 315_316 \ 336_337

311 \ 335_336

这些字符串表示页码列表,例如字符串“309 \ 306 \ 308 \ 337_339”表示 第309,306,308,337,338,339页

我想将这些字符串中的一个传递给函数,将其作为字符串返回 309,306,308,337,338,339

这个功能可以做到但是在c#中,我想在android中实现impalement

 private static string Get_PageNumbers(string str)
    {
        ArrayList arrAll = new ArrayList();
        MatchCollection match;
        string[] excar;
        string strid, firstNumber, lastlNumber;
        int fn, ln;
        ArrayList arrID = new ArrayList();

        //***In Case The Range Number Between "_"
        if (str.Contains("_"))
        {
            // match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
            Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
            match = matchReg.Matches(str);

            int count = match.Count;
            excar = new string[0];
            for (int i = 0; i < count; i++)
            {
                Array.Resize(ref excar, count);

                excar[i] = match[i].Groups[0].Value;
                if (excar[i] != string.Empty)
                    arrID.Add(excar[i]);
            }

            //******IF Array Contains Range Of Number Like"102_110"
            if (str.Contains("_"))
            {
                for (int i = 0; i < arrID.Count; i++)
                {
                    strid = arrID[i].ToString();

                    if (arrID[i].ToString().Contains("_"))
                    {
                        int idy = strid.LastIndexOf("_");
                        firstNumber = strid.Substring(0, idy);

                        if (idy != -1)
                        {
                            lastlNumber = strid.Substring(idy + 1);
                            fn = int.Parse(firstNumber);
                            arrAll.Add(fn);
                            ln = int.Parse(lastlNumber);
                            for (int c = fn; c < ln; c++)
                            {

                                fn++;
                                arrAll.Add(fn);

                            }
                        }
                    }
                    else
                    {
                        arrAll.Add(arrID[i].ToString());
                    }
                }

                //******If Array Contain More Than One Number
                if (arrAll.Count > 0)
                {
                    str = "";
                    for (int i = 0; i < arrAll.Count; i++)
                    {

                        if (str != string.Empty)
                            str = str + "," + arrAll[i];
                        else
                            str = arrAll[i].ToString();
                    }

                }
            }
        }

        //***If string Contains between "/" only without "_"

        else if (str.Contains("/") && !str.Contains("_"))
        {
            str = str.Replace("/", ",");
        }
        else if (str.Contains("\\"))
        {
            str = str.Replace("\\", ",");
        }
        return str;
    }

2 个答案:

答案 0 :(得分:1)

我认为使用split函数更容易:

public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
    String result = "";
    String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
    for (int i = 0; i < pages.length; i++) {
        String page = pages[i];
        int index = page.indexOf('_');
        if (index != -1) { // special case i.e. "337_338", index = 3
            int start = Integer.parseInt(page.substring(0, index)); // start = 337
            int end = Integer.parseInt(page.substring(index + 1)); // end = 338
            for (int j = start; j <= end; j++) {
                result += String.valueOf(j);
                if (j != end) { // don't add ',' after last one
                    result += ",";
                }
            }
        } else { // regular case i.e. "309","306","308"
            result += page;
        }
        if (i != (pages.length-1)) { // don't add ',' after last one
         result += ",";
        }
    }
    return result; // result = "309,306,308,337,338"
}

例如,如下调用此函数:

String result1 = Get_PageNumbers("309\\306\\308\\337_338");
String result2 = Get_PageNumbers("311\\315_316\\336_337");
String result3 = Get_PageNumbers("310");

返回:

309,306,308,337,338
311,315,316,336,337
310

答案 1 :(得分:0)

如果我可以建议不同的实施....

  1. 首先,用“\”str.split("\\");拆分字符串,这里你会收到一个带有单个数字的数组字符串或类似“num_num”的模式
  2. 对于所有创建的字符串,如果字符串NOT包含“”char,则将字符串放入另一个数组(othArr命名),然后再用“str.split("_");拆分,现在你已经一个2位阵列
  3. 转换整数
  4. 中的2个字符串
  5. 现在创建一个最小值的最大值或两个转换后的字符串(并将其放入othArr)
  6. 用“,”
  7. 分隔的字符串中的变换othArr