0

我需要在数千个文件中找到一个字符串,但每一行都有细微的差别,所以我需要正则表达式的帮助

我正在寻找的线路是

<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>

并且除了 alt 标签外,它总是一样的,所以在上面的例子中,“Title Loans Antelope Valley - Online Chat”是独一无二的。

谁能帮我一个正则表达式,它会在 alt 标签“”之间找到任何东西

4

6 回答 6

1

像这样的模式应该有效:

alt="([^"]*)"

这将匹配一个 literal alt=",后跟零个或多个字符,而不是"在第 1 组中捕获的字符,然后是一个 literal "

于 2013-09-10T15:48:26.350 回答
0
preg_match('#<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="(.*?)">#', $string, $matches);

alt 属性将在$matches[1].

于 2013-09-10T15:48:42.797 回答
0
   (?<=alt=")[^"]*

这给了你 and 之间的东西alt="closing "没有alt=" and "

于 2013-09-10T15:50:12.007 回答
0

试试看:

<?php
$lines =  array(
    '<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 1"/>',
    '<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 2"/>',
    '<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 3"/>',
    '<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 4"/>',
    '<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 5"/>'
);

$alt_array = array();
foreach($lines as $line) {
    $alt_array[] = getSubstring($line, 'alt="', '"');   
}

print_r($alt_array);

function getSubstring($input, $start, $end)
{
    preg_match("~".preg_quote($start)."(.*?)".preg_quote($end)."~", $input, $output);
    return $output[1];
}
?>

输出:

Array
(
    [0] => Title Loans Antelope Valley - Online Chat 1
    [1] => Title Loans Antelope Valley - Online Chat 2
    [2] => Title Loans Antelope Valley - Online Chat 3
    [3] => Title Loans Antelope Valley - Online Chat 4
    [4] => Title Loans Antelope Valley - Online Chat 5
)
于 2013-09-10T16:03:23.877 回答
0
(?:<img.+alt=")([^"]+)(?:"\/>)

将产生:

Array
(
    [0] => Array
        (
            [0] => <img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>
        )

    [1] => Array
        (
            [0] => Title Loans Antelope Valley - Online Chat
        )

)

或者更多属性:

(?:<img\s)(?:src=")([^"]+)(?:"\swidth=")([^"]+)(?:"\sheight=")([^"]+)(?:"\sborder=")([^"]+)(?:"\salt=")([^"]+)(?:"\/>)

将产生:

Array
(
    [0] => Array
        (
            [0] => <img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>
        )

    [1] => Array
        (
            [0] => images/online-chat.jpg
        )

    [2] => Array
        (
            [0] => 350
        )

    [3] => Array
        (
            [0] => 150
        )

    [4] => Array
        (
            [0] => 0
        )

    [5] => Array
        (
            [0] => Title Loans Antelope Valley - Online Chat
        )

)
于 2013-09-10T15:54:35.840 回答
0

你也可以使用lookahead 和lookbehind 来选择值,像这样:

(?<=alt=")[^"]*(?=")
于 2013-09-10T15:55:59.463 回答