使用php-preg_match提取具有正则表达式的数字和符号字符串


Extract a string of numbers and symbols with regular expression with using php preg_match

如果我想从类似http://example.com/blabla-bla-i123213131345/blabla 的URL中提取"-i12321331345",我必须使用什么正则表达式

尝试-i[0-9]+模式。

样品:

$str = 'http://example.com/blabla-bla-i123213131345/blabla';
$pattern = '/-i[0-9]+/';
preg_match($pattern, $str, $matches);
var_dump($matches);

演示

在实现正则表达式之前,这里有一个有用的工具可以用来处理它们。学习Regex也有很大帮助。