Php 第二次斜杠后的预匹配查找
这是我的字符串:Php 第二次斜杠后的预匹配查找,php,regex,preg-match,Php,Regex,Preg Match,这是我的字符串: stringa/stringb/123456789,abc,cde 赛前赛后: preg_match('/(?<=\/).*?(?=,)/',$array,$matches); 如何更改preg_匹配以在第二个斜杠(或最后一个斜杠)后提取字符串 期望输出: 123456789 您可以将/以外的任何内容匹配为 /(?<=\/)[^\/,]*(?=,)/ 这应该可以做到 <?php $array = 'stringa/stringb/123456789,a
stringa/stringb/123456789,abc,cde
赛前赛后:
preg_match('/(?<=\/).*?(?=,)/',$array,$matches);
如何更改preg_匹配以在第二个斜杠(或最后一个斜杠)后提取字符串
期望输出:
123456789
您可以将
/
以外的任何内容匹配为
/(?<=\/)[^\/,]*(?=,)/
这应该可以做到
<?php
$array = 'stringa/stringb/123456789,abc,cde';
preg_match('~.*/(.*?),~',$array,$matches);
echo $matches[1];
?>
忽略所有内容,直到最后一个正斜杠(
*/
)。找到最后一个正斜杠后,保留所有数据直到第一个逗号((.*),
)为止。您不需要使用lookback,即:
$string = "stringa/stringb/123456789,abc,cde";
$string = preg_replace('%.*/(.*?),.*%', '$1', $string );
echo $string;
//123456789
演示:
正则表达式解释:
.*/(.*?),.*
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “/” literally «/»
Match the regex below and capture its match into backreference number 1 «(.*?)»
Match any single character that is NOT a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “,” literally «,»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
$1
Insert the text that was last matched by capturing group number 1 «$1»
谢谢你,这很有效!它不应该是[…]而不是
,
或/
?@Driver在[^]
中给出的任何内容都将被否定。因此,要匹配除、
或/
以外的任何内容,我们应该将其命名为[^,\/]
$string = "stringa/stringb/123456789,abc,cde";
$string = preg_replace('%.*/(.*?),.*%', '$1', $string );
echo $string;
//123456789
.*/(.*?),.*
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “/” literally «/»
Match the regex below and capture its match into backreference number 1 «(.*?)»
Match any single character that is NOT a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “,” literally «,»
Match any single character that is NOT a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
$1
Insert the text that was last matched by capturing group number 1 «$1»