Regular expression – you need to know the logic behind several regular expressions

I have a code as

$wrk = OC192-1-1-1;

@temp = split (/-/, $wrk);

if ($temp1[3] =~ /101 || 102 /)
{
print " yes";
} else {
print "no";
}

Output:

yes 

I need to know why this is printing. I know that regular expressions | OR operator support. But I need to know why || gives "yes" as output

This is because || makes the regular expression match successfully by always matching anything.

So it basically Matches any of the following $temp1 [3] (does not exist)

>"101"
>""
>"102"

I add double quotes Just to explain.

I have a code as

$wrk = OC192-1-1- 1;

@temp = split (/-/, $wrk);

if ($temp1[3] =~ /101 || 102 /)
{
print "yes";
} else {
print "no";
}

Output:

yes

Need to know why this is printing yes. I know that regular expressions | OR operator support. But need to know why || gives "yes" as output

This is because || makes the regular expression match successfully by always matching anything.

So it basically matches any of the following One matches $temp1 [3] (does not exist)

>"101"
>""
>"102"

I added double quotes just for explanation.

Leave a Comment

Your email address will not be published.