php 从php正则表达式中提取匹配项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:48:40  来源:igfitidea点击:

Extracting matches from php regex

phpregexperl

提问by Natkeeran

In perl regex we can extract the matched variables, ex below.

在 perl regex 中,我们可以提取匹配的变量,例如下面。

   # extract hours, minutes, seconds
   $time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format
   $hours = ;
   $minutes = ;
   $seconds = ;

How to do this in php?

如何在 php 中做到这一点?

$subject = "E:[email protected] I:100955";
$pattern = "/^E:/";
if (preg_match($pattern, $subject)) {
    echo "Yes, A Match";
}

How to extract the email from there? (We can explode it and get it...but would like a method to get it directly through regex)?

如何从那里提取电子邮件?(我们可以分解它并得到它......但想要一种直接通过正则表达式获取它的方法)?

回答by Chris Rasco

Try using the named subpattern syntax of preg_match:

尝试使用 preg_match 的命名子模式语法:

<?php

$str = 'foobar: 2008';

// Works in PHP 5.2.2 and later.
preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

// Before PHP 5.2.2, use this:
// preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);

print_r($matches);

?>

Output:

输出:

 Array (
     [0] => foobar: 2008
     [name] => foobar
     [1] => foobar
     [digit] => 2008
     [2] => 2008 )

回答by Yannick Motton

Check the php manual

检查php手册

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches1will have the text that matched the first captured parenthesized subpattern, and so on.

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )

如果提供了匹配项,则填充搜索结果。$matches[0] 将包含与完整模式匹配 的文本,$matches 1将包含与第一个捕获的带括号的子模式匹配的文本,依此类推。

$subject = "E:[email protected] I:100955";
$pattern = "/^E:(?<contact>\w+) I:(?<id>\d+)$/";
if (preg_match($pattern, $subject,$matches)) {
    print_r($matches);
}

回答by Josh Davis

You can just modify your current regexp to capture everything after the colon up to the first space:

您可以修改当前的正则表达式以捕获冒号之后到第一个空格的所有内容:

$subject = "E:[email protected] I:100955";
$pattern = "/^E:([^ ]+)/";
if (preg_match($pattern, $subject, $m)) {
    echo "Yes, A Match";
}
$email = $m[1];

In case you're not familiar with regexp, [^ ]+means "any character but a space"and it doesn't require a space to be present to work. If for any reason the input changes to "E:[email protected]"without the " I:12345"bit, it will still work.

如果您不熟悉正则表达式,则[^ ]+表示“除空格外的任何字符”,并且不需要空格即可工作。如果由于任何原因输入更改为没有“I:12345”位的“E:[email protected],它仍然可以工作。

回答by ennuikiller

Use the matchs parameter of the preg_match function as follows:

使用 preg_match 函数的matchs参数如下:

matches:
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

匹配:
如果提供匹配,则填充搜索结果。$matches[0] 将包含与完整模式匹配的文本,$matches[1] 将包含与第一个捕获的带括号的子模式匹配的文本,依此类推。

回答by Juan Sebastian Contreras Aceve

The simpler solution

更简单的解决方案

  1. go to regex101.com
  2. Use the great documentation for creating and testing your regex (make sure you select PHP).
  3. In the TOOLSsection click on code generation

  4. This is an example of what I got.

  1. regex101.com
  2. 使用伟大的文档来创建和测试您的正则表达式(确保您选择 PHP)。
  3. 工具部分点击code generation

  4. 这是我得到的一个例子。

$re = '/\#.*@hello\((?<operator>\w+),?(?<args>.*)\).*/m';
$str = ' Testing string
# @hello(group_fields)
# @hello(operator, arguments, more arguments)';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);