在如下所示的 ASCII 图像中:
....X....... ..X..X...X.... X.X...X..X..... X....XXXXXX..... X..XXX........... .....X.......... ..............X ..X...........X.... ..X...........X....X... ....X.....
我们的目标是检测以下内容模式:
X X X
其中三个 X 垂直对齐。
是的,以下正则表达式可以识别垂直 X 形态的存在:
(?xm) # ignore comments and whitespace, ^ matches beginning of line ^ # beginning of line (?: . # any character except \n (?= # lookahead .*+\n # go to next line ( ?+ . ) # add a character to the 1st capturing group .*+\n # next line ( ?+ . ) # add a character to the 2nd capturing group ) )*? # repeat as few times as needed X .*+\n # X on the first line and advance to next line ?+ # if 1st capturing group is defined, use it, consuming exactly the same number of characters as on the first line X .*+\n # X on the 2nd line and advance to next line ?+ # if 2st capturing group is defined, use it, consuming exactly the same number of characters as on the first line X # X on the 3rd line
在线演示: https://regex101.com/r/YxPeXe/1
间接解
要计算编队的数量,我们可以执行以下替换:
regex =>
其中正则表达式是上面的 图案。生成的字符串长度将等于匹配数。
在线演示: https://regex101.com/r/Tx6R63/1
以上是正则表达式可以检测和计算 ASCII 艺术中的垂直'X”模式吗?的详细内容。更多信息请关注PHP中文网其他相关文章!