Perl regexps doesn't match line with leading zeros / incorrectly escaped digits with leading zeros on command line in Perl

I updated this question since in the original question the problem I was pursuing turned out to be an all-encompassing other error (not interesting in this context). But the second-order error that I spent in testing is that others may come across and give an answer with a very interesting understanding, so I will leave this here as a question.

I was trying to identify a problem with regular expressions that don't seem to match due to leading zeros. I found that all of the following regexp do not match in my command line tests:

"005630" =~ /^0056(10|11|15|20|21|25|30|31)$/ "005630" =~ /0056(10|11|15|20|21|25|30|31)/ "005630" =~ /56(10|11|15|20|21|25|30|31)/ "005630" =~ /..56(10|11|15|20|21|25|30|31)/ "005630" =~ /..5630/ "005630" =~ /005630/ "005630" =~ /^005630$/ "005630" =~ /5630/ "005630" =~ /(0)*5630/ "005630" =~ /5630/g "005630" =~ m/5630/g 

This corresponded to:

 "x005630" =~ /0056(10|11|15|20|21|25|30|31)/ 

similar to others, i.e. as soon as I added the lead letter, it works.

Test code was tested with Cygwin Perl v5.10.1 on Cygwin bash):

 perl -e "print ( "005630" =~ /0056(10|11|15|20|21|25|30|31)/)" # does not display a true value perl -e "print ( "x005630" =~ /0056(10|11|15|20|21|25|30|31)/)" # displays a true value 

The quote here is obviously an error (you cannot use unescaped " in the line specified by " ). But I still do not understand why the second line works, despite the incorrect quoting.

Note. This can happen in other situations without regular expressions.

+4
source share
1 answer

The reason the commands are given

 perl -e "print ( "005630" =~ /0056(10|11|15|20|21|25|30|31)/)" perl -e "print ( "x005630" =~ /0056(10|11|15|20|21|25|30|31)/)" 

only the second line prints a match that Perl supports octal numeric literals . As you understand, your shell feeds on quotes, so you follow the instructions:

 print ( 005630 =~ /0056(10|11|15|20|21|25|30|31)/); print ( x005630 =~ /0056(10|11|15|20|21|25|30|31)/); 

Any numeric literal (unquoted number) starting from zero, immediately followed by a decimal point, is treated as an octal number.

 perl -e "print 005630 . ''" # prints 2968 perl -e "print x005630 . ''" # prints x005630 

(This is required here . '' To ensure that this word is treated as a string. The =~ operator does this in your example.)

So the reason your regex doesn't match is because your string does not contain what you think.

+10
source

Source: https://habr.com/ru/post/1447745/


All Articles