【问题标题】:Extract a string of numbers and symbols with regular expression with using php preg_match [duplicate]使用 php preg_match 使用正则表达式提取一串数字和符号 [重复]
【发布时间】:2013-06-23 07:12:20
【问题描述】:

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

【问题讨论】:

  • 我尝试过类似 '/(-i _[0-9])/' 但没有成功
  • 为什么会这样?里面没有下划线.../-i\d+/
  • 模式中没有量词
  • 非常感谢/-i\d+/。它非常适合我

标签: php preg-match


【解决方案1】:

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

示例:

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

Demo

Here's 在实现正则表达式之前使用它们的有用工具。学习Regex也很有帮助。

【讨论】:

  • 新用户需要付出这么多的努力:P
  • 非常感谢。它非常适合我
  • @Jari 问题格式不正确,但这是新用户的错误。 ;)
猜你喜欢
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2016-05-29
  • 1970-01-01
相关资源
最近更新 更多