在 ASCII 图像中,例如:
....X....... ..X..X...X.... X.X...X..X..... X....XXXXXX..... X..XXX........... .....X.......... ..............X ..X...........X.... ..X...........X....X... ....X.....
我们'我想找到一个由三个 X 组成的简单垂直线结构,而不使用除正则表达式之外的最佳方法。
确定 PCRE/ 中是否存在这样的结构PHP、Perl、.NET 或类似的正则表达式,我们可以使用以下表达式:
(?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
简单匹配
For直接匹配并要求计数为匹配数,由于lookbehind支持有限,这个问题无法在PCRE或Perl中直接解决。
长度/间接解决方案
但是,如果答案被接受为匹配或替换的长度,然后可以用以下表达式回答:
^ (?: (?: # match .+? characters . (?= # counting the same number on the following two lines .*+\n ( ?+ . ) .*+\n ( ?+ . ) ) )+? (?<= X ) # till the above consumes an X (?= # that matches the following conditions .*+\n ?+ (?<= X ) .*+\n ?+ (?<= X ) ) (?= # count the number of matches .*+\n ( ?+ . ) # the number of matches = length of ) )* # repeat as long as there are matches on this line .*\n? # remove the rest of the line
以上是您可以使用正则表达式在 ASCII 图像中找到由三个'X”组成的垂直线吗?的详细内容。更多信息请关注PHP中文网其他相关文章!