[英]Regex Multiple Matches PHP
I'm trying to get all numbers from a string, having -
or _
before the number and optional -
_
space
or the end of string at the end of the number. 我想从一个字符串获取所有数字,有
-
或_
数量和可选的前-
_
space
或字符串在数年底结束。
So, my regex looks like this: 因此,我的正则表达式如下所示:
[-_][\d]+[-_ $]?
My problem is, I don't match numbers right after each other. 我的问题是,我彼此之间不匹配数字。 From a "foo-5234_2123_54-13-20" string, I only get
5234
, 54
and 20
. 从“富-5234_2123_54-13-20”的字符串,我只得到
5234
, 54
和20
。
What I tried is the following regexes: (?:[-_])[\\d]+(?:[-_ $])?
我尝试了以下正则表达式:
(?:[-_])[\\d]+(?:[-_ $])?
and [-_]([\\d]+)[-_ $]?
和
[-_]([\\d]+)[-_ $]?
that obviously didn't work. 那显然没有用。 I'm looking for hours now and I know it can't be that hard so I hope someone can help me here.
我现在正在找几个小时,我知道这不会那么难,所以我希望有人可以在这里帮助我。
If that makes any difference, I'm using PHP preg_match_all
. 如果有什么不同,我正在使用PHP
preg_match_all
。
You just need to use look-arounds : 您只需要使用环顾四周 :
(?<=[-_])\d+(?=[-_ ]|$)
Fortunately, PHP supports at least fixed-width look-behinds, and we can use it here. 幸运的是,PHP至少支持固定宽度的look-hinds,我们可以在这里使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.