在perl正则表达式中,我们可以提取匹配的变量,如下所示.
# extract hours, minutes, seconds
$time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format
$hours = $1;
$minutes = $2;
$seconds = $3;
Run Code Online (Sandbox Code Playgroud)
如何在PHP中执行此操作?
$subject = "E:contact@customer.com I:100955";
$pattern = "/^E:/";
if (preg_match($pattern, $subject)) {
echo "Yes, A Match";
}
Run Code Online (Sandbox Code Playgroud)
如何从那里提取电子邮件?(我们可以将它爆炸并获得它......但是想要一种直接通过正则表达式获取它的方法)?
Chr*_*sco 26
尝试使用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);
?>
Run Code Online (Sandbox Code Playgroud)
输出:
Array (
[0] => foobar: 2008
[name] => foobar
[1] => foobar
[digit] => 2008
[2] => 2008 )
Run Code Online (Sandbox Code Playgroud)
查看php 手册
int preg_match(string $ pattern,string $ subject [,array&$ matches [,int $ flags [,int $ offset]]])
如果提供了匹配,那么它将填充搜索结果.$ matches [0]将包含与完整模式匹配 的文本,$ matches 1将包含与第一个捕获的带括号的子模式匹配的文本,依此类推.
$subject = "E:contact@customer.com I:100955";
$pattern = "/^E:(?<contact>\w+) I:(?<id>\d+)$/";
if (preg_match($pattern, $subject,$matches)) {
print_r($matches);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23591 次 |
最近记录: |