正则表达式表示字符之间的“.


Regex for chars between "

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

我正在寻找的行是

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

除了alt标签之外,它将始终相同,因此在上述情况下,"标题贷款羚羊谷 - 在线聊天"是独一无二的。

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

这样的模式应该有效:

alt="([^"]*)"

这将匹配文字alt=",后跟除第 1 组中捕获"以外的零个或多个字符,后跟文字"

preg_match('#<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="(.*?)">#', $string, $matches);

alt 属性将在 $matches[1] 中。

   (?<=alt=")[^"]*

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

您还可以使用前瞻和后视来挑选值,如下所示:

(?<=alt=")[^"]*(?=")
(?:<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
        )
)

试试看:

<?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
)