微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

regex匹配字符串中所有* texttexttext

如何解决regex匹配字符串中所有* texttexttext

| 我需要一个正则表达式,它将匹配以*开头的所有字符串,直到遇到< 因此在此文本块中:
bob went to the *store and he bought a toy and <then he went outside *and went to his car for <a ride.
它会匹配
bob went to the *store and he bought a toy and
and went to his car for
如果没有\“ <\”,它将匹配所有行,直到行尾     

解决方法

        尝试这个:
<pre>
<?
$s = \'bob went to the *store and he bought a toy and <then he went outside *and went to his car for <a ride.\';

preg_match_all(\"/\\*([^<]+)/\",$s,$matched); 
print_r($matched);
?>
输出:
Array
(
    [0] => Array
        (
            [0] => *store and he bought a toy and 
            [1] => *and went to his car for 
        )

    [1] => Array
        (
            [0] => store and he bought a toy and 
            [1] => and went to his car for 
        )

)
    ,        应该是这样的: 使用PHP:
preg_match_all(\"#\\*(.+?)<#\",$stringWithText,$matches,PREG_SET_ORDER);
$mCount = count($matches);

foreach ($matches as $match)
    echo \"Matched: \" . $match[1] . \"<br/>\";
如果要跳过结尾\“ <\”,则将表达式更改为
#\\*(.+?)<?#
,并且如果要允许更改行,请使用以下标志:
preg_match_all(\"#\\*(.+?)<#si\",PREG_SET_ORDER);
注意表达式后面的si标志 希望能帮助到你     ,        试试
\\*(.+?)<
您可以使用此工具尝试使用正则表达式:http://gskinner.com/RegExr/ 编辑 要与字符串的最后一个“ 9” OR匹配,请使用:
\\*(.+?)[<|$.*]
    ,        我会用这样的东西
(?<=\\*).*?(?=<|$)
在Regexr上查看
(?<=\\*)
是后面的字符,它不匹配任何字符,但可以确保前面的字符是
*
.*?
匹配所有非贪婪的事物
(?=<|$)
是向前看的字符,它不匹配任何字符,但可以确保后面的字符是
<
$
(行尾)==>使用m(多行)修饰符,否则
$
仅匹配字符串,而不是行尾。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。