正则表达式模式差异 - “*"和“+" [英] Regex Pattern Differences - "*" and "+"
本文介绍了正则表达式模式差异 - “*"和“+"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试替换 inch
上的 "
ie 12" wall
将变成 12 inch wall
>
我有 2 个工作模式:
/\b([0-9]+)"/-> preg_replace('/\b([0-9]+)"/', '$1 inch ', $string)
和
/\b([0-9]*)"/-> preg_replace('/\b([0-9]*)"/', '$1 inch ', $string)
那么它们之间有什么区别,为什么 +
和 *
在这里工作方式相同?
干杯,/马辛
解决方案
+ 表示查找前一个字符/组 1 次或多次.
* 表示查找前一个字符/组任意次数(0-无穷大)
I am trying to replace "
on inch
i.e. 12" wall
would become 12 inch wall
I have 2 patterns working:
/\b([0-9]+)"/ -> preg_replace('/\b([0-9]+)"/', '$1 inch ', $string)
and
/\b([0-9]*)"/ -> preg_replace('/\b([0-9]*)"/', '$1 inch ', $string)
what is a difference between them then, why +
and *
works same way here ?
cheers, /Marcin
解决方案
The + means find the previous character/group 1 or more times.
The * means find the previous character/group any amount of times (0-infinity)
这篇关于正则表达式模式差异 - “*"和“+"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文