简体   繁体   English

如何匹配包含X和Y之间的数字的行?

[英]How can I match lines that include a number between X and Y?

In the large file, I have lines of the form SIgnal_Catch[0] , where the number in braces ranges from 0 to 223345. I want to extract the lines where the number is between 2239 and 13569. 在大文件中,行的格式为SIgnal_Catch[0] ,其中大括号中的数字范围为0到223345。我想提取数字在2239和13569之间的行。

I tried this: 我尝试了这个:

print "$line" if $line =~ /SIgnal_Catch\[2239-13569]/;

but it didn't match the lines I want. 但它与我想要的线条不匹配。 I also tried this: 我也试过这个:

print "$line" if $line =~ /SIgnal_Catch\[\d+]/;

but it matches too much. 但它匹配太多。 How can I do this? 我怎样才能做到这一点? For this specific problem, I cannot use $1 for the range. 对于此特定问题,我不能将$ 1用于范围。

Don't try to make regex patterns do too much. 不要试图使正则表达式模式做得太多。 You're writing a Perl program, so use Perl 您正在编写Perl程序,因此请使用Perl

This will do as you ask 这将按照您的要求

if ( /SIgnal_Catch\[(\d+)\]/ and $1 >= 2239 and $1 <= 13569 ) {
    ...
}

Since you've said you can't use $1 or $& , here is the regex string I made. 既然您已经说过不能使用$1$& ,这是我制作的正则表达式字符串。 It works, but it's not pretty: 它可以工作,但是不是很漂亮:

$line =~ /SIgnal_Catch\[\b(2239|22[456789]\d|2[3-9]\d{2}|[3-9]\d{3}|(10|11|12)\d{3}|13[0-4]\d{2}|135[0-6]\d)\b\]/;

Like I said in my comments, you basically have to think how the numbers can be represented in a string, and format all the alternatives. 就像我在评论中说的那样,您基本上必须考虑如何用字符串表示数字,并设置所有替代格式的格式。 I tested this string on regex101 and it works. 我在regex101上测试了此字符串,它可以工作。 But if there is even a slight chance that this kind of regex may pop up again in the future, it's worth thinking about whether you can modify the tool (cfr Borodon's response, or my own comments to the original question) rather than just adding more regexes like this one. 但是,即使这种正则表达式将来极有可能再次出现,也值得考虑是否可以修改该工具(CFR Borodon的回答,或我对原始问题的评论),而不仅仅是添加更多这样的正则表达式。

NB: If you do test it on the regex101 web site, and you have several numbers in the Test String box, don't forget to add the global modifier g to the regex, as it will otherwise just find the first. 注意:如果您确实在regex101网站上进行了测试,并且在“测试字符串”框中有多个数字,请不要忘记将全局修饰符g添加到正则表达式中,否则它将找到第一个。

if ( $line =~ /.*SIgnal_Catch\[([1-9][0-9]{3,4})\].*/ ) {

    if ( $1 >= 2239  &&  $1 <= 13569 ) {
       print $line . "\n";
    }

}

Doing what you want with just regex would be, um, really hairy. 则表达式做你想做的事情,真是毛茸茸的。 Try something like the above. 尝试类似上面的方法。

As AntonH mentioned you can use the following below: 正如AntonH所提到的,您可以使用以下内容:

$_ =~ /SIgnal_Catch\[(\d+)\]/;

if ( $1 >= 2239 && $1 <= 13569 )
{
    print $_;
}

Your regex above will not work and your match is missing the "[" and "]". 您上面的正则表达式将不起作用,并且您的匹配项缺少“ [”和“]”。

Hope this helps. 希望这可以帮助。

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

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