简体   繁体   English

PHP RegEx字符串包含多于两个的数字,彼此之间用斜杠括起来

[英]PHP RegEx string contains more than two numbers next to each other enclosed with slashes

My RegEx is poor, and I have been struggling with this. 我的RegEx很差,我一直在为此苦苦挣扎。 I have some URL's that contain a pattern like this: 我有一些包含这样的模式的URL:

/1234/
/5527191/
/15974/

And so on. 等等。 It's always a bunch of letters, then a slash, numbers ( no more than 10 numbers) and then another slash. 它总是一堆字母,然后是一个斜杠,一个数字(不超过10个数字),然后是另一个斜杠。

So I'm after some RegEx that will search, ignore letters and find a group of numbers bewteen two slashes. 因此,我需要一些RegEx,它将搜索,忽略字母并在两个斜杠之间找到一组数字。

Thanks for the help! 谢谢您的帮助!

You can use preg_match_all (or preg_match if you only want the first match) with the following regular expression: 您可以使用preg_match_all (或preg_match如果你只是想在第一场比赛)与以下正则表达式:

preg_match_all("#/\d{1,10}/#", $s, $matches);

Explanation 说明

  • # is the delimiter for the regular expression. #是正则表达式的分隔符
  • / matches a literal slash. /匹配一个斜杠。
  • \\d matches any digit. \\d匹配任何数字。
  • {1,10} matches the previous token between 1 and 10 times. {1,10}与先前的令牌匹配1至10次。

See it working online: ideone 看到它在线上工作: ideone

If you want to also capture the number without the slashes you could use a capturing group: 如果您还想捕获不带斜杠的数字,则可以使用捕获组:

preg_match_all("#/(\d{1,10})/#", $s, $matches);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM