Mounnad Mohamed
Mounnad Mohamed

Reputation: 23

How to combine two regex pattern in perl?

I want to combine two regex pattern to split string and get a table of integers.

this the example :

$string= "1..1188,1189..14,14..15";
$first_pattern = /\../;
$second_pattern = /\,/;

i want to get tab like that:

[1,1188,1189,14,14,15]

Upvotes: -1

Views: 715

Answers (1)

choroba
choroba

Reputation: 241798

Use | to connect alternatives. Also, use qr// to create regex objects, using plain /.../ matches against $_ and assigns the result to $first_pattern and $second_pattern.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my $string = '1..1188,1189..14,14..15';
my $first_pattern = qr/\.\./;
my $second_pattern = qr/,/;

my @integers = split /$first_pattern|$second_pattern/, $string;
say for @integers;

You probably need \.\. to match two dots, as \.. matches a dot followed by anything but a newline. Also, there's no need to backslash a comma.

Upvotes: 1

Related Questions