简体   繁体   English

正则表达式查找结果但忽略注释的 # 行

[英]Regex to find results but ignore the commented # line


I'm trying to use regex in PHP to find something in.ipmconf file. 我正在尝试在 PHP 中使用正则表达式来查找 in.ipmconf 文件中的内容。
And the data inside the file is categorized in each [V //something] section. 并且文件内的数据被分类在每个 [V //something] 部分。 This is my.ipmconf file looks like: 这是 my.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


Currently my code just able to read the data until the "TopHostApplication = some data here" line, and it stops there.目前我的代码只能读取数据,直到“TopHostApplication = some data here”行,它就停在那里。 So many data below that are missing, starting from the first comment.从第一条评论开始,下面有很多数据丢失了。
How to ignore the #comments line and continue search another line until it arrive at last line?如何忽略#comments 行并继续搜索另一行直到它到达最后一行?

here is some part my code:这是我的代码的一部分:

 $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

I'm new with regex and PHP stuff.我是正则表达式和 PHP 东西的新手。 Thank you in advance!先感谢您!

Why not just loop the the entire data, and extract the rows you want?为什么不循环整个数据,然后提取你想要的行呢?

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 :-)
    }
}

Incidentally: placing your entire loop on a single line, in the way you did, makes your code harder to read and has no effect on the size/efficiency of the compiled script whatsoever!顺便说一句:以您的方式将整个循环放在一行上,会使您的代码更难阅读,并且对已编译脚本的大小/效率没有任何影响!

I modified your regex to capture the complete section name.我修改了您的正则表达式以捕获完整的部分名称。 The following should process the entire configuration.下面应该处理整个配置。 I have added some echo statements:我添加了一些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";

Prints:印刷:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM