源字符串:p3a0b0?kw=%e5%bc%a0
问题:我想匹配出?kw前面的a0,而不是kw后面的a0,正则应该怎么写(注意上面p、a、b、?kw=后面的参数都会发生变化)要求:用零宽断言.而不是简单的用p(\d+)a(\d+)这种方式

解决方案 »

  1.   

    可以先截取?kw前的 再去匹配a0
      

  2.   

    MatchCollection allMatchResults = null;
    try {
    Regex regexObj = new Regex(@"a\d+(?=.*kw)");
    allMatchResults = regexObj.Matches("a\d+(?=.*kw)");
    if (allMatchResults.Count > 0) {
    // Access individual matches using allMatchResults.Item[]
    } else {
    // Match attempt failed

    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  3.   

    搞错了MatchCollection allMatchResults = null;
    try {
        Regex regexObj = new Regex(@"a\d+(?=.*kw)");
        allMatchResults = regexObj.Matches("p3a0b0?kw=%e5%bc%a0");
        if (allMatchResults.Count > 0) {
            // Access individual matches using allMatchResults.Item[]
        } else {
            // Match attempt failed
        } 
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }