正则表达式与href字符串不匹配

时间:2015-10-16 07:29:06

标签: php regex

我有以下正则表达式:

    public static void main(String[] args) {
    int i = 5;
    int j = 0;
    for (int k = 1; k <= i; k++) {
        System.out.println("row count is " + j);
        j = increment(j); //  IT is important to store it in `j` again, otherwise j will still be 2 after the execution
        j += 1;
    }
}

private static int increment(int j) {
    if (j == 2) {
        j += 1;
        System.out.println("row count is " + j);
    }
    return j;
}

如何改善这一点,使其与href标签中的“files”或“resize”字词不匹配:

$regex = '<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>';

2 个答案:

答案 0 :(得分:0)

是的,解析是更好的方法来做到这一点 - 也许有人觉得这很有帮助:

$i=0;
$code=$_POST['code'];
$img=$_POST['img'];
$name=$_POST['name'];
$short_description=$_POST['short_description'];


$columns = array(
    if($code){
    array('db' => 'code', 'dt' => $i),
    i++;
    }else if($img){
    array('db' => 'img', 'dt' => $i),
    i++;
    }else if($name){
    array('db' => 'name', 'dt' => $i),
    i++;
    }else if($short_description){
    array('db' => 'short_description', 'dt' => $i),
    }
);

答案 1 :(得分:-1)

您可以使用此正则表达式获取所有href字符串:

<a[^>]*href=[\"\'](.*?)[\"\'][^>]*>(.*?)</a>