我正在处理大量数据,并且我试图返回其中包含字符串“INFO:”的所有行。我已经设法让模式返回我感兴趣的数据,但是想知道如何改进这个正则表达式模式以省略我匹配的字符串(如果可能的话还有空格),所以只返回我感兴趣的实际数据。
$pattern = "/^.*INFO:.*\$/m";
preg_match_all($pattern, $content, $matches);
INFO: framerate 25.00
INFO: width 480.00
INFO: height 360.00
INFO: audioinputvolume 75.00
INFO: audiocodecid mp4a
INFO: audiodatarate 48.00
答案 0 :(得分:2)
preg_match_all('/^INFO:\s+([^\s]+)\s+([^\s]+)$/ms', $content, $matches);
返回:
Array
(
[0] => Array
(
[0] => INFO: framerate 25.00
[1] => INFO: width 480.00
[2] => INFO: height 360.00
[3] => INFO: audioinputvolume 75.00
[4] => INFO: audiocodecid mp4a
[5] => INFO: audiodatarate 48.00
)
[1] => Array
(
[0] => framerate
[1] => width
[2] => height
[3] => audioinputvolume
[4] => audiocodecid
[5] => audiodatarate
)
[2] => Array
(
[0] => 25.00
[1] => 480.00
[2] => 360.00
[3] => 75.00
[4] => mp4a
[5] => 48.00
)
)
请注意,这两个字段都不允许这样有空格。
答案 1 :(得分:1)
$pattern = "/INFO:\s+(.*?)\s+(.*?)(\s|$)/m";
这应该可以解决问题。括号中匹配的内容将在$ matches [1]和$ matches [2]
中显示为元素这是输出的内容:
Array
(
[0] => Array
(
[0] => INFO: framerate 25.00
[1] => INFO: width 480.00
[2] => INFO: height 360.00
[3] => INFO: audioinputvolume 75.00
[4] => INFO: audiocodecid mp4a
[5] => INFO: audiodatarate 48.00
)
[1] => Array
(
[0] => framerate
[1] => width
[2] => height
[3] => audioinputvolume
[4] => audiocodecid
[5] => audiodatarate
)
[2] => Array
(
[0] => 25.00
[1] => 480.00
[2] => 360.00
[3] => 75.00
[4] => mp4a
[5] => 48.00
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
)
)
所有空格/行尾字符都有第三个数组,因为我使用括号来使用|运算符说空格或文本的末尾可以匹配。
答案 2 :(得分:0)
将您感兴趣的群组放在subpattern ( )
我认为在你的情况下它看起来像:
$pattern = "/^.*INFO:(.*)\$/m";
现在,您可以使用$matches[1][$match]