3 # This is a home for regular expression tests that don't fit into
4 # the format supported by re/regexp.t. If you want to add a test
5 # that does fit that format, add it to re/re_tests, not here.
18 @INC = ('../lib','.');
22 plan tests => 474; # Update this when adding/deleting tests.
24 run_tests() unless caller;
33 (my $x_pretty = $x) =~ s/\n/\\n/g;
35 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
36 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
38 # used to be a test for $*
39 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
41 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
42 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
44 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
45 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
47 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
48 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
50 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
51 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
56 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
61 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
62 qq [\$_ = '$_'; /(a*b*)(c*)/];
63 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
64 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
67 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
68 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
71 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
72 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
75 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
76 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
77 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
78 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
82 # used to be a test for $*
83 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
87 our %XXX = map {($_ => $_)} 123, 234, 345;
89 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
90 while ($_ = shift(@XXX)) {
91 my $e = index ($_, 'not') >= 0 ? '' : 1;
109 ok !keys %XXX, "%XXX is empty";
115 my $message = "Test empty pattern";
121 is($&, $xyz, $message);
126 is($&, $xyz, $message);
130 no warnings 'uninitialized';
132 is($&, $xyz, $message);
136 is($&, $xyz, $message);
140 my $message = q !Check $`, $&, $'!;
142 /def/; # optimized up to cmd
143 is("$`:$&:$'", 'abc:def:ghi', $message);
146 /cde/ + 0; # optimized only to spat
147 is("$`:$&:$'", 'ab:cde:fghi', $message);
149 /[d][e][f]/; # not optimized
150 is("$`:$&:$'", 'abc:def:ghi', $message);
154 $_ = 'now is the {time for all} good men to come to.';
156 is($1, 'time for all', "Match braces");
160 my $message = "{N,M} quantifier";
161 $_ = 'xxx {3,4} yyy zzz';
162 ok(/( {3,4})/, $message);
163 is($1, ' ', $message);
164 unlike($_, qr/( {4,})/, $message);
165 ok(/( {2,3}.)/, $message);
166 is($1, ' y', $message);
167 ok(/(y{2,3}.)/, $message);
168 is($1, 'yyy ', $message);
169 unlike($_, qr/x {3,4}/, $message);
170 unlike($_, qr/^xxx {3,4}/, $message);
174 my $message = "Test /g";
176 $_ = "now is the time for all good men to come to.";
177 my @words = /(\w+)/g;
178 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
180 is("@words", $exp, $message);
186 is("@words", $exp, $message);
193 is("@words", "to:to", $message);
197 is("@words", "to:to", $message);
213 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
214 my $t6 = my $t7 = my $t8 = my $t9 = 0;
216 for my $iter (1 .. 5) {
227 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
228 is($x, '505550555', "Test /o");
233 ok "abc" =~ /^abc$|$xyz/, "| after \$";
235 # perl 4.009 says "unmatched ()"
236 my $message = '$ inside ()';
239 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
240 is($@, "", $message);
241 is($result, "abc:bc", $message);
245 my $message = "Scalar /g";
248 ok( /abc/g && $` eq "", $message);
249 ok( /abc/g && $` eq "abcfoo", $message);
250 ok(!/abc/g, $message);
252 $message = "Scalar /gi";
254 ok( /ABC/gi && $` eq "", $message);
255 ok( /ABC/gi && $` eq "abcfoo", $message);
256 ok(!/ABC/gi, $message);
258 $message = "Scalar /g";
260 ok( /abc/g && $' eq "fooabcbar", $message);
261 ok( /abc/g && $' eq "bar", $message);
265 is(@x, 2, "/g reset after assignment");
269 my $message = '/g, \G and pos';
273 is(pos $_, 2, $message);
275 is(pos $_, undef, $message);
279 my $message = '(?{ })';
281 'abc' =~ m'a(?{ $out = 2 })b';
282 is($out, 2, $message);
285 'abc' =~ m'a(?{ $out = 3 })c';
286 is($out, 1, $message);
290 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
291 my @out = /(?<!foo)bar./g;
292 is("@out", 'bar2 barf', "Negative lookbehind");
296 my $message = "REG_INFTY tests";
297 # Tests which depend on REG_INFTY
299 # Defaults assumed if this fails
300 eval { require Config; };
301 $::reg_infty = $Config::Config{reg_infty} // 32767;
302 $::reg_infty_m = $::reg_infty - 1;
303 $::reg_infty_p = $::reg_infty + 1;
304 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
306 # As well as failing if the pattern matches do unexpected things, the
307 # next three tests will fail if you should have picked up a lower-than-
308 # default value for $reg_infty from Config.pm, but have not.
310 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
311 is($@, '', $message);
312 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
313 is($@, '', $message);
314 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
315 is($@, '', $message);
317 eval "'aaa' =~ /a{1,$::reg_infty}/";
318 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
319 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
320 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
324 # Poke a couple more parse failures
325 my $context = 'x' x 256;
326 eval qq("${context}y" =~ /(?<=$context)y/);
327 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
332 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
334 my $message = "Long monster, length = $l";
335 like("ba$a=", qr/a$a=/, $message);
336 unlike("b$a=", qr/a$a=/, $message);
337 like("b$a=", qr/ba+=/, $message);
339 like("ba$a=", qr/b(?:a|b)+=/, $message);
344 # 20000 nodes, each taking 3 words per string, and 1 per branch
345 my $long_constant_len = join '|', 12120 .. 32645;
346 my $long_var_len = join '|', 8120 .. 28645;
347 my %ans = ( 'ax13876y25677lbc' => 1,
348 'ax13876y25677mcb' => 0, # not b.
349 'ax13876y35677nbc' => 0, # Num too big
350 'ax13876y25677y21378obc' => 1,
351 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
352 'ax13876y25677y21378y21378kbc' => 1,
353 'ax13876y25677y21378y21378kcb' => 0, # Not b.
354 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
358 my $message = "20000 nodes, const-len '$_'";
359 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
361 $message = "20000 nodes, var-len '$_'";
362 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
367 my $message = "Complicated backtracking";
368 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
369 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
376 (?{ $c = 1 }) # Initialize
378 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
380 ) # Fail: will unwind one iteration back
383 [^()]+ # Match a big chunk
386 ) # Do not try to match subchunks
394 )+ # This may not match with different subblocks
399 ) # Otherwise the chunk 1 may succeed with $c>0
405 push @ans, $res while $res = matchit;
406 is("@ans", "1 1 1", $message);
409 is("@ans", $expect, $message);
411 $message = "Recursion with (??{ })";
413 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
415 @ans = my @ans1 = ();
416 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
418 is("@ans", "1 1 1", $message);
419 is("@ans1", $expect, $message);
422 is("@ans", $expect, $message);
427 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
431 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
432 is("@ans", 'a/ b', "Stack may be bad");
436 my $message = "Eval-group not allowed at runtime";
437 my $code = '{$blah = 45}';
440 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
443 my $res = eval { "xx" =~ /(?$code)/o };
445 no warnings 'uninitialized';
446 chomp $@; my $message = "$message '$@', '$res', '$blah'";
447 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
452 $res = eval { "xx" =~ /(?$code)/o };
454 no warnings 'uninitialized';
455 my $message = "$message '$@', '$res', '$blah'";
456 ok(!$@ && $res, $message);
459 $code = '{$blah = 45}';
462 is($blah, 45, $message);
466 is($blah, 45, $message);
470 my $message = "Pos checks";
473 is(pos $x, 2, $message);
476 is(pos $x, 2, $message);
484 is(f (pos $x), 4, $message);
488 my $message = 'Checking $^R';
490 'foot' =~ /foo(?{$x = 12; 75})[t]/;
491 is($^R, 75, $message);
494 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
495 ok($^R eq '67' && $x eq '12', $message);
498 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
499 ok($^R eq '79' && $x eq '12', $message);
503 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
504 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
505 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
506 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
507 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
508 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
511 { # Test that charset modifier work, and are interpolated
512 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
513 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
514 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
515 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
516 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
518 my $dual = qr/\b\v$/;
520 my $locale = qr/\b\v$/;
521 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
524 use feature 'unicode_strings';
525 my $unicode = qr/\b\v$/;
526 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
527 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
528 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
530 no feature 'unicode_strings';
531 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
532 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
535 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
536 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
540 my $message = "Look around";
542 foreach my $ans ('', 'c') {
543 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
544 is($1, $ans, $message);
549 my $message = "Empty clause";
551 foreach my $ans ('', 'a', '') {
552 ok(/^|a|$/g, $message);
553 is($&, $ans, $message);
559 my $message = "Prefixify";
561 my ($v, $a, $b, $res) = @_;
562 ok($v =~ s/\Q$a\E/$b/, $message);
563 is($v, $res, $message);
567 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
568 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
574 ok $1 && /$1/, "Capture a quote";
578 no warnings 'closure';
579 my $message = '(?{ $var } refers to package vars';
583 '' =~ /(?{ $c = 4 })/;
584 main::is($c, 4, $message);
585 main::is($::c, 3, $message);
589 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
590 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
591 'POSIX class [: :] must have valid name');
593 for my $d (qw [= .]) {
594 is(eval "/[[${d}foo${d}]]/", undef);
595 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
596 "POSIX syntax [[$d $d]] is an error");
601 # test if failure of patterns returns empty list
602 my $message = "Failed pattern returns empty list";
605 is("@_", "", $message);
608 is("@_", "", $message);
611 is("@_", "", $message);
614 is("@_", "", $message);
618 my $message = '@- and @+ tests';
621 is($#+, 0, $message);
622 is($#-, 0, $message);
623 is($+ [0], 2, $message);
624 is($- [0], 1, $message);
625 ok(!defined $+ [1] && !defined $- [1] &&
626 !defined $+ [2] && !defined $- [2], $message);
629 is($#+, 2, $message);
630 is($#-, 2, $message);
631 is($+ [0], 3, $message);
632 is($- [0], 0, $message);
633 is($+ [1], 2, $message);
634 is($- [1], 1, $message);
635 is($+ [2], 3, $message);
636 is($- [2], 2, $message);
637 ok(!defined $+ [3] && !defined $- [3] &&
638 !defined $+ [4] && !defined $- [4], $message);
640 # Exists has a special check for @-/@+ - bug 45147
641 ok(exists $-[0], $message);
642 ok(exists $+[0], $message);
643 ok(exists $-[2], $message);
644 ok(exists $+[2], $message);
645 ok(!exists $-[3], $message);
646 ok(!exists $+[3], $message);
647 ok(exists $-[-1], $message);
648 ok(exists $+[-1], $message);
649 ok(exists $-[-3], $message);
650 ok(exists $+[-3], $message);
651 ok(!exists $-[-4], $message);
652 ok(!exists $+[-4], $message);
655 is($#+, 3, $message);
656 is($#-, 3, $message);
657 is($+ [1], 2, $message);
658 is($- [1], 1, $message);
659 is($+ [3], 3, $message);
660 is($- [3], 2, $message);
661 ok(!defined $+ [2] && !defined $- [2] &&
662 !defined $+ [4] && !defined $- [4], $message);
665 is($#+, 1, $message);
666 is($#-, 1, $message);
667 is($+ [0], 2, $message);
668 is($- [0], 0, $message);
669 is($+ [1], 2, $message);
670 is($- [1], 1, $message);
671 ok(!defined $+ [2] && !defined $- [2] &&
672 !defined $+ [3] && !defined $- [3], $message);
675 is($#+, 2, $message);
676 is($#-, 1, $message);
679 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)',
680 '@- = qw (foo bar)', '$^N = 42') {
682 like($@, qr/^Modification of a read-only value attempted/,
683 '$^N, @- and @+ are read-only');
687 my $message = '\G testing';
691 is("@a", "a a", $message);
695 unlike($str, qr/^\G/, $message);
696 unlike($str, qr/^.\G/, $message);
697 like($str, qr/^..\G/, $message);
698 unlike($str, qr/^...\G/, $message);
699 ok($str =~ /\G../ && $& eq 'cd', $message);
701 local $::TODO = $::running_as_thread;
702 ok($str =~ /.\G./ && $& eq 'bc', $message);
706 my $message = 'pos inside (?{ })';
709 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
710 is($foo, $str, $message);
711 is($bar, 2, $message);
712 is(pos $str, undef, $message);
717 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
718 is($foo, $str, $message);
719 is($bar, 2, $message);
720 is(pos $str, 3, $message);
725 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
726 is($foo, $str, $message);
727 is($bar, 2, $message);
731 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
732 is($foo, $str, $message);
733 is($bar, 2, $message);
734 is(pos, 3, $message);
739 1 while /b(?{$foo = $_; $bar = pos})c/g;
740 is($foo, $str, $message);
741 is($bar, 2, $message);
742 is(pos, undef, $message);
747 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
748 is($foo, 'abcde|abcde', $message);
749 is($bar, 8, $message);
750 is($_, 'axde|axde', $message);
755 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
756 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
757 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
760 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
761 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
762 is("@res", "'' 'ab' 'cde|abcde' " .
763 "'' 'abc' 'de|abcde' " .
764 "'abcd' 'e|' 'abcde' " .
765 "'abcde|' 'ab' 'cde' " .
766 "'abcde|' 'abc' 'de'", $message);
770 my $message = '\G anchor checks';
771 my $foo = 'aabbccddeeffgg';
774 local $::TODO = $::running_as_thread;
775 no warnings 'uninitialized';
776 ok($foo =~ /.\G(..)/g, $message);
777 is($1, 'ab', $message);
780 ok($foo =~ /.\G(..)/g, $message);
781 is($1, 'cc', $message);
784 ok($foo =~ /.\G(..)/g, $message);
785 is($1, 'de', $message);
787 ok($foo =~ /\Gef/g, $message);
791 ok($foo =~ /\G(..)/g, $message);
792 is($1, 'aa', $message);
794 ok($foo =~ /\G(..)/g, $message);
795 is($1, 'bb', $message);
798 ok($foo =~ /\G(..)/g, $message);
799 is($1, 'cd', $message);
804 my @res = /(\d*|x)/g;
806 is("@res", "123||x|123|", "0 match in alternation");
810 my $message = "Match against temporaries (created via pp_helem())" .
812 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
813 is($1, "bar", $message);
817 my $message = 'package $i inside (?{ }), ' .
818 'saved substrings and changing $_';
819 our @a = qw [foo bar];
821 s/(\w)(?{push @b, $1})/,$1,/g for @a;
822 is("@b", "f o o b a r", $message);
823 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
825 $message = 'lexical $i inside (?{ }), ' .
826 'saved substrings and changing $_';
827 no warnings 'closure';
828 my @c = qw [foo bar];
830 s/(\w)(?{push @d, $1})/,$1,/g for @c;
831 is("@d", "f o o b a r", $message);
832 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
836 my $message = 'Brackets';
839 { (?> [^{}]+ | (??{ $brackets }) )* }
842 ok("{{}" =~ $brackets, $message);
843 is($&, "{}", $message);
844 ok("something { long { and } hairy" =~ $brackets, $message);
845 is($&, "{ and }", $message);
846 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
847 is($&, "{ and }", $message);
853 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
857 my $message = '\G anchor checks';
858 my $text = "aaXbXcc";
860 ok($text !~ /\GXb*X/g, $message);
865 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
867 my $text = "abc dbf";
868 my @res = ($text =~ /.*?(b).*?\b/g);
869 is("@res", "b b", '\b is not special');
873 my $message = '\S, [\S], \s, [\s]';
874 my @a = map chr, 0 .. 255;
875 my @b = grep m/\S/, @a;
876 my @c = grep m/[^\s]/, @a;
877 is("@b", "@c", $message);
880 @c = grep /[\S]/, @a;
881 is("@b", "@c", $message);
884 @c = grep /[^\S]/, @a;
885 is("@b", "@c", $message);
888 @c = grep /[\s]/, @a;
889 is("@b", "@c", $message);
892 my $message = '\D, [\D], \d, [\d]';
893 my @a = map chr, 0 .. 255;
894 my @b = grep /\D/, @a;
895 my @c = grep /[^\d]/, @a;
896 is("@b", "@c", $message);
899 @c = grep /[\D]/, @a;
900 is("@b", "@c", $message);
903 @c = grep /[^\D]/, @a;
904 is("@b", "@c", $message);
907 @c = grep /[\d]/, @a;
908 is("@b", "@c", $message);
911 my $message = '\W, [\W], \w, [\w]';
912 my @a = map chr, 0 .. 255;
913 my @b = grep /\W/, @a;
914 my @c = grep /[^\w]/, @a;
915 is("@b", "@c", $message);
918 @c = grep /[\W]/, @a;
919 is("@b", "@c", $message);
922 @c = grep /[^\W]/, @a;
923 is("@b", "@c", $message);
926 @c = grep /[\w]/, @a;
927 is("@b", "@c", $message);
931 # see if backtracking optimization works correctly
932 my $message = 'Backtrack optimization';
933 like("\n\n", qr/\n $ \n/x, $message);
934 like("\n\n", qr/\n* $ \n/x, $message);
935 like("\n\n", qr/\n+ $ \n/x, $message);
936 like("\n\n", qr/\n? $ \n/x, $message);
937 like("\n\n", qr/\n*? $ \n/x, $message);
938 like("\n\n", qr/\n+? $ \n/x, $message);
939 like("\n\n", qr/\n?? $ \n/x, $message);
940 unlike("\n\n", qr/\n*+ $ \n/x, $message);
941 unlike("\n\n", qr/\n++ $ \n/x, $message);
942 like("\n\n", qr/\n?+ $ \n/x, $message);
947 use overload '""' => sub {'Object S'};
950 my $message = "Ref stringification";
951 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
952 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
953 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
954 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
955 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
959 my $message = "Test result of match used as match";
960 ok('a1b' =~ ('xyz' =~ /y/), $message);
961 is($`, 'a', $message);
962 ok('a1b' =~ ('xyz' =~ /t/), $message);
963 is($`, 'a', $message);
967 my $message = '"1" is not \s';
968 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
969 undef, "$message (did not warn)");
973 my $message = '\s, [[:space:]] and [[:blank:]]';
974 my %space = (spc => " ",
979 # There's no \v but the vertical tabulator seems miraculously
980 # be 11 both in ASCII and EBCDIC.
984 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
985 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
986 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
988 is("@space0", "cr ff lf spc tab", $message);
989 is("@space1", "cr ff lf spc tab vt", $message);
990 is("@space2", "spc tab", $message);
995 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
996 # seem to be predictable. Slight changes to the test make it fail earlier or later.
997 foreach my $i (0 .. $n)
1000 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
1004 # we are actually testing that we dont die when executing these patterns
1007 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1009 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1010 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1011 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1012 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1015 # we are actually testing that we dont die when executing these patterns
1016 my $e = "B\x{f6}ck";
1017 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1019 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1020 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1021 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1022 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1027 # Tests for bug 77414.
1030 my $message = '\p property after empty * match';
1032 like("1", qr/\s*\pN/, $message);
1033 like("-", qr/\s*\p{Dash}/, $message);
1034 like(" ", qr/\w*\p{Blank}/, $message);
1037 like("1", qr/\s*\pN+/, $message);
1038 like("-", qr/\s*\p{Dash}{1}/, $message);
1039 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
1043 SKIP: { # Some constructs with Latin1 characters cause a utf8 string not
1044 # to match itself in non-utf8
1046 skip "Needs to be customized to run on EBCDIC", 6;
1049 my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/;
1050 utf8::upgrade($utf8_pattern);
1051 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
1052 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
1053 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
1054 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
1056 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
1057 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
1058 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
1059 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
1062 SKIP: { # Make sure can override the formatting
1064 skip "Needs to be customized to run on EBCDIC", 2;
1066 use feature 'unicode_strings';
1067 ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1068 ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1072 # Test that a regex followed by an operator and/or a statement modifier work
1073 # These tests use string-eval so that it reports a clean error when it fails
1074 # (without the string eval the test script might be unparseable)
1076 # Note: these test check the behaviour that currently is valid syntax
1077 # If a new regex modifier is added and a test fails then there is a backwards-compatibility issue
1078 # Note-2: a new deprecate warning was added for this with commit e6897b1a5db0410e387ccbf677e89fc4a1d8c97a
1079 # which indicate that this syntax will be removed in 5.16.
1080 # When this happens the tests can be removed
1082 foreach (['my $r = "a" =~ m/a/lt 2', 'm', 'lt'],
1083 ['my $r = "a" =~ m/a/le 1', 'm', 'le'],
1084 ['my $r = "a" =~ m/a/eq 1', 'm', 'eq'],
1085 ['my $r = "a" =~ m/a/ne 0', 'm', 'ne'],
1086 ['my $r = "a" =~ m/a/and 1', 'm', 'and'],
1087 ['my $r = "a" =~ m/a/unless 0', 'm', 'unless'],
1088 ['my $c = 1; my $r; $r = "a" =~ m/a/while $c--', 'm', 'while'],
1089 ['my $c = 0; my $r; $r = "a" =~ m/a/until $c++', 'm', 'until'],
1090 ['my $r; $r = "a" =~ m/a/for 1', 'm', 'for'],
1091 ['my $r; $r = "a" =~ m/a/foreach 1', 'm', 'foreach'],
1093 ['my $t = "a"; my $r = $t =~ s/a//lt 2', 's', 'lt'],
1094 ['my $t = "a"; my $r = $t =~ s/a//le 1', 's', 'le'],
1095 ['my $t = "a"; my $r = $t =~ s/a//ne 0', 's', 'ne'],
1096 ['my $t = "a"; my $r = $t =~ s/a//and 1', 's', 'and'],
1097 ['my $t = "a"; my $r = $t =~ s/a//unless 0', 's', 'unless'],
1099 ['my $c = 1; my $r; my $t = "a"; $r = $t =~ s/a//while $c--', 's', 'while'],
1100 ['my $c = 0; my $r; my $t = "a"; $r = $t =~ s/a//until $c++', 's', 'until'],
1101 ['my $r; my $t = "a"; $r = $t =~ s/a//for 1', 's', 'for'],
1102 ['my $r; my $t = "a"; $r = $t =~ s/a//for 1', 's', 'foreach'],
1104 my $message = sprintf 'regex (%s) followed by $_->[2]',
1105 $_->[1] eq 'm' ? 'm//' : 's///';
1106 my $code = "$_->[0]; 'eval_ok ' . \$r";
1108 no warnings 'syntax';
1111 is($@, '', $message);
1112 is($result, 'eval_ok 1', $message);
1120 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
1123 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
1128 local $::TODO = "[perl #38133]";
1130 "A" =~ /(((?:A))?)+/;
1136 is($first, $second);
1140 # RT #3516: \G in a m//g expression causes problems
1142 while ("abc" =~ m/(\G[ac])?/g) {
1143 last if $count++ > 10;
1145 ok($count < 10, 'RT #3516 A');
1148 while ("abc" =~ m/(\G|.)[ac]/g) {
1149 last if $count++ > 10;
1151 ok($count < 10, 'RT #3516 B');
1154 while ("abc" =~ m/(\G?[ac])?/g) {
1155 last if $count++ > 10;
1157 ok($count < 10, 'RT #3516 C');
1160 # RT #84294: Is this a bug in the simple Perl regex?
1161 # : Nested buffers and (?{...}) dont play nicely on partial matches
1163 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
1164 my $want= "'ab', 'a', 'b'";
1165 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1166 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1170 # Suppress warnings, as the non-unicode one comes out even if turn off
1171 # warnings here (because the execution is done in another scope).
1172 local $SIG{__WARN__} = sub {};
1173 my $str = "\x{110000}";
1175 # No non-unicode code points match any Unicode property, even inverse
1177 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{}");
1178 unlike($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode doesn't match \\p{}");
1179 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{}");
1180 like($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{}");
1184 # Test that IDstart works, but because the author (khw) knows
1185 # regexes much better than the rest of the core, it is being done here
1186 # in the context of a regex which relies on buffer names beginng with
1190 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1191 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1192 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1193 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1195 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1196 # IDStarts there, but are not Word characters, and therefore Perl
1197 # doesn't allow them to be IDStarts. But there is no guarantee that
1198 # Unicode won't change things around in the future so that at some
1199 # future Unicode revision these tests would need to be revised.
1200 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1203 "abc" =~ qr/(?<$char>abc)/;
1205 utf8::encode($prog);
1206 fresh_perl_like($prog, qr!Sequence.* not recognized!, "",
1207 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1213 utf8::upgrade($pat);
1214 like("\xffb", qr/$pat/i, "/i: utf8 pattern, non-utf8 string, latin1-char preceding matching char in string");
1217 { # Crash with @a =~ // warning
1218 local $SIG{__WARN__} = sub {
1219 pass 'no crash for @a =~ // warning'
1221 eval ' sub { my @a =~ // } ';
1224 { # Concat overloading and qr// thingies
1230 '""' => sub { ${$_[0]} },
1232 push @refs, ref $_[1] if ref $_[1];
1233 bless $_[2] ? \"$_[1]${$_[0]}" : \"${$_[0]}$_[1]"
1238 my $o = bless \$s, Cat::;
1240 is "@refs", "Regexp", '/$o$qr/ passes qr ref to cat overload meth';
1246 $count++ while $str=~/.*/g;
1247 is $count, 2, 'test that ANCH_MBOL works properly. We should get 2 from $count++ while "\n"=~/.*/g';
1249 $class_count++ while $str=~/[^\n]*/g;
1250 is $class_count, $count, 'while "\n"=~/.*/g and while "\n"=~/[^\n]*/g should behave the same';
1252 $anch_count++ while $str=~/^.*/mg;
1253 is $anch_count, 1, 'while "\n"=~/^.*/mg should match only once';
1258 like "\xe0", qr/(?i:\xc0)/, "(?i: shouldn't lose the passed in /u";
1260 unlike "\x{100}", qr/(?i:\w)/, "(?i: shouldn't lose the passed in /a";
1262 unlike 'k', qr/(?i:\N{KELVIN SIGN})/, "(?i: shouldn't lose the passed in /aa";
1264 } # End of sub run_tests