首页 / 正则 / php – 从结尾匹配的正则表达式
php – 从结尾匹配的正则表达式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 从结尾匹配的正则表达式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1321字,纯文字阅读大概需要2分钟。
内容图文
![php – 从结尾匹配的正则表达式](/upload/InfoBanner/zyjiaocheng/723/77e68345c2f74efe8cf3e033f400772d.jpg)
你好我需要匹配一个字符串从结尾(从右到左).例如从字符串hello999hello888hello777last我需要在最后一组hello和last之间获得777.Which从下面的代码正常工作.
$game = "hello999hello888hello777last";
preg_match('/hello(\d+)last$/', $game, $match);
print_r($match);
但是,而不是777,我有符号数字和字母的混合,假设例如来自字符串hello999hello888hello0string#@ $@#anysymbols%@ iwantlast我需要带0string#@ $@#anysymbols%@ iwant.
$game = "hello999hello888hello0string#@$@#anysymbols%@iwantlast";
preg_match('/hello(.*?)last$/', $game, $match);
print_r($match);
为什么上面的代码会重复999hello888hello0string#@ $@#%#$%#$%#$%@ iwant.从右到左读取正确的过程,然后是字符串反向方法.
注意:我想使用preg_match_all aswel.for匹配多个字符串
$string = 'hello999hello888hello0string#@$@#anysymbols%@iwantlast
hello999hello888hello02ndstring%@iwantlast';
preg_match_all('/.*hello(.*?)last$/', $string, $match);
print_r($match);
必须返回0string#@ $@#anysymbols%@ iwant和02ndstring%@ iwant
解决方法:
尝试更改你的正则表达式:
/.*hello(.*?)last$/
说明:
.* eat everything before the last 'hello' (it's greedy)
hello eat the last hello
(.*?) capture the string you want
last and finally, stop at 'last'
$ anchor to end
的?实际上是不必要的,因为如果你要锚定到最后,你还是想要最后一个.如果你想匹配像helloMatch这样的textlast并不匹配,那么删除$.
对于多行,只需删除$.
内容总结
以上是互联网集市为您收集整理的php – 从结尾匹配的正则表达式全部内容,希望文章能够帮你解决php – 从结尾匹配的正则表达式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。