正则表达式以获取信息

时间:2012-10-11 18:47:55

标签: php regex

我有一个字符串,我想为它提取一些信息。 字符串可以是这样的

$string = "Followers: abc.com. ID by: xyz@gmail.com. More info: all the rest of information goes here. All other goes everywhere else."

$ string有时只有

$string = "ID by: xyz@gmail.com."

$string = "Followers: abc.com."

或任何其他组合。我只是想看看那里是否有ID并将其解决。

实现这一目标的最佳方法是什么

1 个答案:

答案 0 :(得分:2)

使用带preg_match的正则表达式查找ID。

preg_match('/ID by\\: ([\\w@\\.]+)\\.(?: |$)/',$input,$matches);

ID(电子邮件地址)将位于$matches[1]。模式匹配“ID by:”,捕获电子邮件地址,最后需要句点+空格或字符串结尾。