简体   繁体   中英

Regular Expression Numeric Range Fails in Perl

My script needs to find a specific linux kernel version using regular expressions - 3.13.0-24-generic.

I tried:

'((2\.[46])|(3\.[0-99])\.\d[^\/]*?) \(.*@.*\) [#]\d+.*'

but it doesn't work. How can I match my kernel version? Why does the regex above fail to match 3.13.0-24-generic?

Additional Info Based on Comments:
I am editing a SystemImager UseYourOwnKernel.pm script. The original script checks for either a 2.4 or 2.6 kernel:

my $regex =
#           | kernel version + build machine
#           `---------------------------------------
            '(2\.[46]\.\d[^\/]*?) \(.*@.*\) [#]\d+.*' .

3.0,1,2, (ie single digit 3.X kernels) work with:

'((2\.[46])|(3\.[012])\.\d[^\/]*?) \(.*@.*\) [#]\d+.*'

I can get it to match the 3.13 and (3.0, 3.1, 3.2, etc) kernel using:

'((2\.[46])|(3\.[012]|3\.13)\.\d[^\/]*?) \(.*@.*\) [#]\d+.*' .

I need a numeric range that will match from 3.0 to 3.99 (this should take care of all 3.X releases).

Your regex would be,

(?:[0-9]|[1-9][0-9])\.(?:[0-9]|[1-9][0-9])\.(?:[0-9]|[1-9][0-9])-(?:[0-9]|[1-9][0-9])-\w+

DEMO

Explanation:

  • (?:[0-9]|[1-9][0-9]) Matches any number from 0-99.

  • \\. A literal dot.

  • - A literal -

OR

A much shorter one,

(?:(?:[0-9]|[1-9][0-9])\.){2}(?:(?:[0-9]|[1-9][0-9])-){2}\w+

DEMO

此代码可以帮助您:

(?:(?:[\d]|[1-9][\d])\.){2}(?:(?:[0-9]|[1-9][0-9])-){2}\w+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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