PHP预匹配过滤
我正在学习正则表达式,我不知道如何从输出中“提取”特定的内容 示例-我想检索特定CSS样式的值 下面是一个简化的示例:PHP预匹配过滤,php,regex,preg-match,Php,Regex,Preg Match,我正在学习正则表达式,我不知道如何从输出中“提取”特定的内容 示例-我想检索特定CSS样式的值 下面是一个简化的示例: $source = 'foo { bar: Something } foo { bar: Else } foo { bar: Yay }'; 我想在var_dump之后输出: array(3) { [0]=> string(9) "Something" [1]=> string(4) "Else" [
$source = 'foo { bar: Something }
foo { bar: Else }
foo { bar: Yay }';
我想在var_dump之后输出:
array(3) {
[0]=>
string(9) "Something"
[1]=>
string(4) "Else"
[2]=>
string(3) "Yay"
}
这是我的正则表达式:
preg_match_all("/foo\s*{\s*bar:\s*[A-Za-z]*\s*}/",$source,$matches);
foreach($matches as $example) {
echo '<pre>';
var_dump($example);
echo '</pre>';
}
如何限制我的输出数据,使其仅显示所需的内容,而不是所有与正则表达式匹配的内容
[编辑]
伙计们,我喜欢一些解释而不是工作示例,我真的很想学习这件事!:)谢谢强>
/foo\s*{\s*bar:\s*([A-Za-z]*)\s*}/
本例中的括号称为“捕获组”
本例中的括号称为“捕获组”
在要匹配的区域周围使用括号。在要匹配的区域周围使用括号。尝试将正则表达式更改为
$text = 'Here comes a number: 5, here comes a number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a number: ([0-9])/', $text, $matches );
然后再次查看输出。然后,您可能会在输出中看到只包含要获取的文本的条目
通过使用(
和)
在正则表达式中创建一个组,preg_match_all函数也将仅在这些组中输出内容
输出阵列
例如:
array(
array( 'Here comes a number: 5', '5' ),
array( 'Here comes a number: 5', '5' ),
array( 'Here comes a number: 5', '5' )
)
运行此代码后,$matches
现在将为:
$text = 'Here comes a number: 5, here comes another number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a(nother)? number: ([0-9])/', $text, $matches );
如您所见,$matches
将为匹配的每个字符串包含一个数组。第一个条目($matches[0]
)将始终包含完整的匹配字符串。其他索引($matches[1]
,$matches[2]
等)将仅按顺序包含指定组的值。如果指定可选组(例如test([0-9])?
),则关联索引将包含null
值
从输出中排除组
有时,您希望指定一个组,但不希望将其包含在输出数组中。例如:
$text = 'Here comes a number: 5, here comes a number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a(?:nother)? number: ([0-9])/', $text, $matches );
我为另一个添加了一个组,因为我希望它是可选的。现在,我的$matches[1]
包含“其他”
或null
,我的$matches[2]
包含实际数字。因为我对用户选择写“另一个”还是“a”不感兴趣,所以我想从输出中排除这个组
这可以通过使用(?:
启动组来完成。生成的代码:
组
(?:其他)
在输出中被忽略,并且$匹配[1]
对我们感兴趣的实际数字的引用。尝试将正则表达式更改为
$text = 'Here comes a number: 5, here comes a number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a number: ([0-9])/', $text, $matches );
然后再次查看输出,您可能会在输出中看到只包含要获取的文本的条目
通过使用(
和)
在正则表达式中创建一个组,preg_match_all函数也将仅在这些组中输出内容
输出阵列
例如:
array(
array( 'Here comes a number: 5', '5' ),
array( 'Here comes a number: 5', '5' ),
array( 'Here comes a number: 5', '5' )
)
运行此代码后,$matches
现在将为:
$text = 'Here comes a number: 5, here comes another number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a(nother)? number: ([0-9])/', $text, $matches );
如您所见,$matches
将包含每个匹配字符串的数组。第一个条目($matches[0]
)将始终包含完整的匹配字符串。其他索引($matches[1]
,$matches[2]
等等)将仅按顺序包含指定组的值。如果指定可选组(例如test([0-9])?
),则关联索引将包含null
值
从输出中排除组
有时,您希望指定组,但不希望将其包含在输出数组中。例如:
$text = 'Here comes a number: 5, here comes a number: 3
and here comes a number: 4';
preg_match_all( '/[Hh]ere comes a(?:nother)? number: ([0-9])/', $text, $matches );
我为另一个添加了一个组,因为我希望它是可选的。现在我的$matches[1]
包含“other”
或null
,而我的$matches[2]
包含实际的数字。因为我对用户选择写“other”还是“a”不感兴趣,我想从输出中排除此组
这可以通过使用(?:
启动组来完成。生成的代码:
组(?:其他)
在输出中被忽略,$匹配[1]
引用我们感兴趣的实际数字