如下所示的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中文網其他相關文章!