Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 第二次斜杠后的预匹配查找_Php_Regex_Preg Match - Fatal编程技术网

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»