正则表达式查找结果但忽略注释 # 行
Regex to find results but ignore the commented # line
我正在尝试在 PHP 中使用正则表达式来查找 .ipmconf 文件中的内容。
并且文件中的数据在每个 [V //something] 部分中进行了分类。
这是我的 .ipmconf 文件,如下所示:
[V5 sentry tuning]
LossThreshold = some data here
TopHostApplication = some data here
#rt_comp_level : compression type allowed for "Real Time" flows
rt_comp_level = some data here
#tr_comp_level : compression type allowed for "Transactionnal" flows
tr_comp_level = some data here
#bg_comp_level : compression type allowed for "background" flows
bg_comp_level = some data here
tunnelcomport = some data here
[V6 something]
//other data here
目前我的代码只能读取数据,直到 "TopHostApplication = some data here" 行,它就停在那里。从第一个评论开始,下面有很多数据缺失。
如何忽略#comments 行并继续搜索另一行直到到达最后一行?
这是我的部分代码:
$filename = $_FILES['form']['name']['config'];
$conf = file("./components/com_rsform/uploads/config".$filename);
$buf = implode("\n",$conf);
preg_match('/domain = (.*)/m',$buf,$match);
$domain = $match[1];
unlink("./com_rsform/uploads/config".$filename);
while(!empty($conf)){
set_time_limit(240);
$line = array_shift ($conf);
if(preg_match('/^\[V\d+ (.*)\]/',$line,$match)){$section = $match[1];}
if ($section == "sentry tuning"){
$data1 ="";$data2 ="";
while (preg_match('/^(.*) = (.*)$/',$line,$matches)){
$data1 = $matches[1];
$data2 = $matches[2];
$line = array_shift($conf);}
$section = "";
}
elseif($section == "[V6 something]")//next section
{//codes
}
}
//echo $data1 and $data2 somewhere here
我是正则表达式和 PHP 方面的新手。提前致谢!
为什么不循环整个数据,然后提取所需的行?
foreach ($conf as $line) {
if(preg_match('/^(.*) = (.*)$/',$line,$matches)){
$data1 = $matches[1];
$data2 = $matches[2];
//You presumably wanted to do something with $data1 and $data2 after this, but that's not in your example code :-)
}
}
顺便说一下:按照您的方式将整个循环放在一行中会使您的代码更难阅读,并且对已编译脚本的 size/efficiency 没有任何影响!
我修改了您的正则表达式以捕获完整的部分名称。下面应该处理整个配置。我添加了一些 echo
语句:
while (!empty($conf)) {
$line = array_shift($conf);
if (!preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // slightly modified regex to get the complete section name
continue;
}
$section = $match[1];
echo "section = $section\n";
if ($section == "V5 sentry tuning") {
while (!empty($conf)) { // processes lines up until the next section
$line = array_shift($conf);
if (preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // start of a new section
array_unshift($conf, $line); // put the line back to be processed by the next section
break;
}
if (substr($line, 0, 1) == '#') { // a comment
continue;
}
if (preg_match('/^(.*) = (.*)$/', $line, $matches)) {
$data1 = $matches[1];
$data2 = $matches[2];
echo "data1 = $data1, data2 = $data2\n";
}
}
}
elseif ($section == 'V6 something') {
// do something here
}
}
echo "End of processing\n";
打印:
section = V5 sentry tuning
data1 = LossThreshold, data2 = some data here
data1 = TopHostApplication, data2 = some data here
data1 = rt_comp_level, data2 = some data here
data1 = tr_comp_level, data2 = some data here
data1 = bg_comp_level, data2 = some data here
data1 = tunnelcomport, data2 = some data here
section = V6 something
End of processing
我正在尝试在 PHP 中使用正则表达式来查找 .ipmconf 文件中的内容。
并且文件中的数据在每个 [V //something] 部分中进行了分类。 这是我的 .ipmconf 文件,如下所示:
[V5 sentry tuning]
LossThreshold = some data here
TopHostApplication = some data here
#rt_comp_level : compression type allowed for "Real Time" flows
rt_comp_level = some data here
#tr_comp_level : compression type allowed for "Transactionnal" flows
tr_comp_level = some data here
#bg_comp_level : compression type allowed for "background" flows
bg_comp_level = some data here
tunnelcomport = some data here
[V6 something]
//other data here
目前我的代码只能读取数据,直到 "TopHostApplication = some data here" 行,它就停在那里。从第一个评论开始,下面有很多数据缺失。
如何忽略#comments 行并继续搜索另一行直到到达最后一行?
这是我的部分代码:
$filename = $_FILES['form']['name']['config'];
$conf = file("./components/com_rsform/uploads/config".$filename);
$buf = implode("\n",$conf);
preg_match('/domain = (.*)/m',$buf,$match);
$domain = $match[1];
unlink("./com_rsform/uploads/config".$filename);
while(!empty($conf)){
set_time_limit(240);
$line = array_shift ($conf);
if(preg_match('/^\[V\d+ (.*)\]/',$line,$match)){$section = $match[1];}
if ($section == "sentry tuning"){
$data1 ="";$data2 ="";
while (preg_match('/^(.*) = (.*)$/',$line,$matches)){
$data1 = $matches[1];
$data2 = $matches[2];
$line = array_shift($conf);}
$section = "";
}
elseif($section == "[V6 something]")//next section
{//codes
}
}
//echo $data1 and $data2 somewhere here
我是正则表达式和 PHP 方面的新手。提前致谢!
为什么不循环整个数据,然后提取所需的行?
foreach ($conf as $line) {
if(preg_match('/^(.*) = (.*)$/',$line,$matches)){
$data1 = $matches[1];
$data2 = $matches[2];
//You presumably wanted to do something with $data1 and $data2 after this, but that's not in your example code :-)
}
}
顺便说一下:按照您的方式将整个循环放在一行中会使您的代码更难阅读,并且对已编译脚本的 size/efficiency 没有任何影响!
我修改了您的正则表达式以捕获完整的部分名称。下面应该处理整个配置。我添加了一些 echo
语句:
while (!empty($conf)) {
$line = array_shift($conf);
if (!preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // slightly modified regex to get the complete section name
continue;
}
$section = $match[1];
echo "section = $section\n";
if ($section == "V5 sentry tuning") {
while (!empty($conf)) { // processes lines up until the next section
$line = array_shift($conf);
if (preg_match('/^\[(V\d+ .*)\]/',$line,$match)) { // start of a new section
array_unshift($conf, $line); // put the line back to be processed by the next section
break;
}
if (substr($line, 0, 1) == '#') { // a comment
continue;
}
if (preg_match('/^(.*) = (.*)$/', $line, $matches)) {
$data1 = $matches[1];
$data2 = $matches[2];
echo "data1 = $data1, data2 = $data2\n";
}
}
}
elseif ($section == 'V6 something') {
// do something here
}
}
echo "End of processing\n";
打印:
section = V5 sentry tuning
data1 = LossThreshold, data2 = some data here
data1 = TopHostApplication, data2 = some data here
data1 = rt_comp_level, data2 = some data here
data1 = tr_comp_level, data2 = some data here
data1 = bg_comp_level, data2 = some data here
data1 = tunnelcomport, data2 = some data here
section = V6 something
End of processing