Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于检查这些模式的正则表达式_Php_Regex - Fatal编程技术网

Php 用于检查这些模式的正则表达式

Php 用于检查这些模式的正则表达式,php,regex,Php,Regex,我在邮件的主题中有这些图案 [!491440]:<some text> [Support !489434]:<some text> [SUPPORT !491430]:<some text> [!491440]: [支持!489434]: [支持!491430]: 我需要的是:- 要获取主题中出现的数字,请在“!”感叹号之后。 我需要的数字(可以是任何数字),从主题(提供样本主题)获取 如何从模式中识别:- 方括号“[”(有时会有“支撑”或“支撑”和空格)

我在邮件的主题中有这些图案

[!491440]:<some text>
[Support !489434]:<some text>
[SUPPORT !491430]:<some text>
[!491440]:
[支持!489434]:
[支持!491430]:
我需要的是:-

要获取主题中出现的数字,请在“!”感叹号之后。 我需要的数字(可以是任何数字),从主题(提供样本主题)获取

如何从模式中识别:-

方括号“[”(有时会有“支撑”或“支撑”和空格)后跟“”然后是数字,然后方括号闭合“

我需要电话号码。上面提供的示例。

(?i)
不区分大小写的修饰符有助于进行不区分大小写的匹配

(?i)\[(?:SUPPORT\s+)?!(\d+)]:
从组索引1中获取所需的号码

您可以使用在期末打印时放弃先前匹配的字符
(?=]:)
称为正向先行断言,断言匹配后必须跟有
]:
字符

(?i)\[(?:SUPPORT\s+)?!\K\d+(?=]:)

试试这个。抓取捕获。参见演示。使用
g
i
标志

$re=“/\\[(?:支持\\s*)?!(\\d+\\]/i”;
$str=“[!491440]:\n[支持!489434]:\n[支持!491430]:”;
预匹配全部($re,$str,$MATCHS);

谢谢,但在这里,“支持在任何情况下都可以”。请参阅已编辑的question@Monisha在我的正则表达式中添加了不区分大小写的修饰符。谢谢@Avinash Raj:)太棒了!!非常感谢
<?php
$str = <<<EOT
[SUPPORT !491430]:Message for Netcetera from Answer-4U
[!491440]:<some text>
[Support !489434]:<some text>
[SUPPORT !491430]:<some text>
EOT;
preg_match_all("~(?i)\[(?:SUPPORT\s+)?!\K\d+(?=]:)~", $str, $matches);
print_r($matches);
?>
Array
(
    [0] => Array
        (
            [0] => 491430
            [1] => 491440
            [2] => 489434
            [3] => 491430
        )

)
\[(?:Support\s*)?!(\d+)\]
$re = "/\\[(?:Support\\s*)?!(\\d+)\\]/i";
$str = "[!491440]:<some text>\n<some text>[Support !489434]:<some text>\n<some text>[SUPPORT !491430]:<some text>";

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