简体   繁体   中英

Perl conditional regex with inequalities

I have one query. I have to match 2 strings in one if condition:

$release = 5.x (Here x should be greater than or equal to 3) $version = Rx (this x should be greater than or equal to 5 if $release is 5.3, otherwise anything is acceptable)

eg 5.1R11 is not acceptable, 5.3R4 is not, 5.3R5 is acceptable, and 5.4 R1 is acceptable.

I have written a code like this:

$release = "5.2";
$version = "R4";

if ( $release =~ /5.(?>=3)(\d)/ && $version =~ m/R(?>=5)(\d)/ )
{
    print "OK";
}

How can I write this?

This is really a three-level version string, and I suggest that you use Perl's version facility

use strict;
use warnings 'all';
use feature 'say';
use version;

my $release = '5.2';
my $version = 'R4';

if ( $version =~ /R(\d+)/ && version->parse("$release.$1") ge v5.3.5 ) {
    say 'OK';
}

In regex (?>) it means atomic grouping.

Group the element so it will stored into $1 then compare the $1 with number so it should be

if (( ($release =~ /5\.(\d)/) && ($1 > 3) ) && (($version =~ m/R(\d)/) && ($1 >= 3) ) )
{
    print "OK\n";
}

I got the correct one after modifying mkhun's solution:

if ((($release =~ /5.3/)) && (($version =~ m/R(\d+)(.\d+)?/) && ($1 >= 5))
|| ((($release =~ /5.(\d)/) && ($1 > 3)) && ($version =~ m/R(\d+)(.\d+)?/)) )
{
    print "OK\n";
}

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