http://qs1969.pair.com?node_id=11138972


in reply to Problem with regex is a bug? or my regex

<update2> Just posting this here for visibility: The solution is further down in the thread. </update2>

Both when filing a bug report and when asking a question here, you'll need to provide a Short, Self-Contained, Correct Example that reproduces the issue. That is, runnable code that includes sample input and expected output. Note that here, the regex you showed and the output you provided do not match (and it doesn't appear you're using a common module such as Data::Dumper to output your strings?). Your regexes would also benefit greatly from the /x modifier. The following code runs fine on Perl 5.18 through 5.34 on my system.

use warnings; use strict; use Test::More tests=>4; my $str = "########################################################### +###################\r\n# This system is a restricted access system. + #\r\n# If collected security informati +on reveals possible criminal activity that #\r\n# exceeds privileges +, evidence of such activity may be provided to the rele- #\r\n# vant +authorities for further action. By continuing past this point, you + #\r\n# expressly consent to this security monitoring. + #\r\n################################################## +############################\r\n\r\nhostname: ~# "; my $re1 = qr{(([#%:>~\$\] ])(?!\2)){3,4}|([\w\-\.]*)\$ *$|\w[@\/]\w.*? +[#%>~\$\]]|^[#%\$>\:]~] *$}; my $re2 = qr{(([#%:>~\$\] ])(?!\2)){3,4}|([\w\-\.]*)\$ *$|(\w[@\/]\w|s +ftp).*?[#%>~\$\]]|^[#%\$>\:]~] *$}; ok $str =~ $re1; is $&, ": ~#"; ok $str =~ $re2; is $&, ": ~#";

Update: After looking at those regexes a little closer, I fail to see how either of them could match "  #" at all: in the first branch, every time a space matches it has to be followed by something that isn't a space or #, and in the second through fourth branches, each potential match of spaces has to be preceded by something that isn't a space, and in the second and fourth branches, the spaces need to be at the end of the line. Perhaps you made a mistake when editing \w[@\/]\w to (\w[@\/]\w|sftp), which could maybe explain the match you observed. Again, please use Data::Dumper with $Data::Dumper::Useqq=1; or Data::Dump to output strings and regexen in a representative manner. Also tweaked test code a tiny bit.

(Update 2: Why is whitespace being compacted in these <code> tags?? "    #" - hm, probably a stylesheet issue) Also clarified wording in the above update.