发现多次出现时,使用正则表达式提取字符串的一部分

时间:2018-10-27 07:21:54

标签: regex

String =“

(a) its failure to pay is caused by:
(i) administrative or technical error; or
(ii)    a Disruption Event; and
(b) [payment is made within:
(i) (in the case of paragraph (a)(i) above), [5] Business Days of its due date; or
(ii)    (in the case of paragraph (a)(ii) above), [1] Business Days of its due date]/
OR
[payment is made within [ 10 ] Business Days of its due date.]

输出:[10]个工作日

我正在尝试的正则表达式= (?<=payment is made within)(.*)(?=of its due date)

但是这些给了我3行,我只希望最后一次出现,任何人都可以帮忙。

我正在尝试的Asp.net中的代码:

        string regularExpressionPattern = RegExPattern.ToString();
        string inputText = FinalPara.ToString();
        Regex re = new Regex(regularExpressionPattern);
        foreach (Match m in re.Matches(FinalPara))
        {
            Response.Write("Regex Values is :" + m.Groups[1].Value.ToString());
        }

1 个答案:

答案 0 :(得分:0)

负前瞻

将搜索模式分为两个捕获组,然后在第二个捕获组上使用负前行。


演示

StringRequest stringRequest = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
            Log.d("response", response.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        Log.d("error", String.valueOf(error));
    }
})
{
    @Override
    public Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        params.put("name", paramName);
        params.put("email", paramEmail);
        return params;
    }
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);