fork(1) download
  1. use v5.12;
  2. use warnings;
  3.  
  4. # demo for:
  5. # http://stackoverflow.com/questions/17039670/vertical-regex-matching-in-an-ascii-image
  6.  
  7.  
  8. my $r = qr/
  9. ^ # beginning of line
  10. (?:
  11. . # any character except \n
  12. (?= # lookahead
  13. .*+\n # go to next line
  14. ( \1?+ . ) # add a character to the 1st capturing group
  15. .*+\n # next line
  16. ( \2?+ . ) # add a character to the 2nd capturing group
  17. )
  18. )*? # repeat as few times as needed
  19. X .*+\n # X on the first line and advance to next line
  20. \1?+ # if 1st capturing group is defined, use it, consuming exactly the same number of characters as on the first line
  21. X .*+\n # X on the 2nd line and advance to next line
  22. \2?+ # if 2st capturing group is defined, use it, consuming exactly the same number of characters as on the first line
  23. X # X on the 3rd line
  24. /xm;
  25.  
  26.  
  27. my @maps = split /\n\n/, <<'MAPS';
  28. X
  29. X
  30. X
  31.  
  32. ..X....
  33. ..X....
  34. ..X....
  35.  
  36. ..X.X..
  37. ..X.X..
  38. ....X..
  39.  
  40. ..X....
  41. ..X....
  42. ...X...
  43.  
  44. ..X....
  45. ...X...
  46. ..X....
  47.  
  48. ....X..
  49. .X..X..
  50. .X.....
  51. MAPS
  52.  
  53.  
  54. for(my $i = 0; $i < @maps; ++$i){
  55. my $map = $maps[$i];
  56. my $m = $map =~ /$r/;
  57. say "map $i:";
  58. say "$map\n";
  59.  
  60. say $m ? "MATCHED:": "no match";
  61. say $& if $m;
  62.  
  63. say "---------------";
  64. }
  65.  
Success #stdin #stdout 0s 3692KB
stdin
Standard input is empty
stdout
map 0:
X
X
X

MATCHED:
X
X
X
---------------
map 1:
..X....
..X....
..X....

MATCHED:
..X....
..X....
..X
---------------
map 2:
..X.X..
..X.X..
....X..

MATCHED:
..X.X..
..X.X..
....X
---------------
map 3:
..X....
..X....
...X...

no match
---------------
map 4:
..X....
...X...
..X....

no match
---------------
map 5:
....X..
.X..X..
.X.....


no match
---------------