Allen Allen - 2 months ago 7
Perl Question

how to match content except and, or, ||, && in perl regex

for example like this"

$str = "1 < 4 and 8 > 2 or 4 * 3 or $m =~ /^\d+&\$/";

I would like to capture

1 < 4

8 > 2

4 * 3

$m =~ /^d+&\$/

however, $str =~ /\s+(?and|or|&&|\|\|){1,}\s+/; doesn't work, any help to modify

Answer

To set $str to that, you should use single quotes (or escape all the meta characters).

my $str = '1 < 4 and 8 > 2 or 4 * 3 or $m =~ /^\d+&\$/';
my @capture = split  /\s+(?:and|or|&&|\|\|)\s+/, $str;
Comments