Php 正则表达式-将组1作为“获取”;“完全匹配”/第0组

Php 正则表达式-将组1作为“获取”;“完全匹配”/第0组,php,regex,preg-match,Php,Regex,Preg Match,给出以下示例,我想获得电子邮件地址 Eg. 1: some standard text. Bugs Bunny bugs@gmail.com 0411111111 more standard text Eg. 2: some standard text. Bugs The Bunny bugs@gmail.com 0411111111 more standard text Eg. 3: some standard text. Bugs-Bunny bugs.bunny@gmail.

给出以下示例,我想获得电子邮件地址

Eg. 1: some standard text.   Bugs Bunny bugs@gmail.com 0411111111 more standard text 
Eg. 2: some standard text.   Bugs The Bunny bugs@gmail.com 0411111111 more standard text
Eg. 3: some standard text.   Bugs-Bunny bugs.bunny@gmail.com 0411111111 more standard text
Eg. 4: some standard text.   Bugs bugs.bunny@gmail.com +6141 111 111 more standard text
Eg. 5: some standard text.   Bugs o'Bunny bugs@gmail.com 0411111111 more standard text 

这样就可以了:
(?如果您将正则表达式更改为简单的([^\s]+@[^\s]+),完整结果应该只是电子邮件地址。

组0是完全匹配的


如果将正则表达式更改为
[^\s]+@[^\s]+
,则它将与所有示例中的电子邮件地址匹配

由于名称的长度不同,您不能使用正向查找并将电子邮件地址作为整个匹配项进行匹配。

您可以执行以下操作:

$lines = array(
"some standard text.   Bugs Bunny bugs@gmail.com 0411111111 more standard text ",
"some standard text.   Bugs The Bunny bugs@gmail.com 0411111111 more standard text",
"some standard text.   Bugs-Bunny bugs.bunny@gmail.com 0411111111 more standard text",
"some standard text.   Bugs bugs.bunny@gmail.com +6141 111 111 more standard text",
"some standard text.   Bugs o'Bunny bugs@gmail.com 0411111111 more standard text ",
);
foreach($lines as $line) {
    preg_match('/some standard text..+?\K\S+@\S+/', $line, $m);
    var_dump($m);
}
其中:

  • \K
    意味着忘记我们在这里遇到的一切
  • \S
    代表任何非空白,与
    [^\S]
那么我们只有
$m[0]

输出:

array(1) {
  [0]=>
  string(14) "bugs@gmail.com"
}
array(1) {
  [0]=>
  string(14) "bugs@gmail.com"
}
array(1) {
  [0]=>
  string(20) "bugs.bunny@gmail.com"
}
array(1) {
  [0]=>
  string(20) "bugs.bunny@gmail.com"
}
array(1) {
  [0]=>
  string(14) "bugs@gmail.com"
}

在给出的示例中,我知道了,但我需要
一些标准文本之后的第一个电子邮件地址。
因为全文中有电子邮件地址。基本上,您要求匹配“一些标准文本”,但在完整结果中不显示。我认为这是不可能的。