preg_match 在第二个斜杠后回溯 [英] preg_match lookbehind after second slash

查看:46
本文介绍了preg_match 在第二个斜杠后回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的字符串:

stringa/stringb/123456789,abc,cde

以及在 preg_match 之后:

and after preg_match:

preg_match('/(?<=\/).*?(?=,)/',$array,$matches);

输出为:

stringb/123456789

如何更改我的 preg_match 以在第二个斜杠之后(或最后一个斜杠之后)提取字符串?

How can I change my preg_match to extract the string after second slash (or after last slash)?

所需的输出:

123456789

推荐答案

您可以将 / 以外的任何内容匹配为

You can match anything other than a / as

/(?<=\/)[^\/,]*(?=,)/

  • [^\/,]* 否定字符类匹配除 \
  • 之外的任何内容

    • [^\/,]* Negated character class matches anything other than , or \
    • 正则表达式演示

      示例

      preg_match('/(?<=\/)[^\/,]*(?=,)/',$array,$matches);
      // $matches[0]
      // => 123456789
      

      这篇关于preg_match 在第二个斜杠后回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆