首頁 >後端開發 >php教程 >您可以使用正規表示式在 ASCII 影像中找到由三個'X”組成的垂直線嗎?

您可以使用正規表示式在 ASCII 影像中找到由三個'X”組成的垂直線嗎?

Patricia Arquette
Patricia Arquette原創
2024-11-08 12:42:02925瀏覽

Can You Find Vertical Lines of Three 'X's in an ASCII Image Using Regex?

ASCII 影像中的垂直正規表示式匹配

問題

在ASCII 影像中,例如:

....X.......
..X..X...X....
X.X...X..X.....
X....XXXXXX.....
X..XXX...........
.....X..........
..............X
..X...........X....
..X...........X....X...
....X.....

在ASCII 影像中,例如:

在ASCII 影像中,例如:

(?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
在ASCII 影像中,例如:

我們'我想找到一個由三個X 組成的簡單垂直線,而不使用除此之外的最佳方法正則表達式。 問題1:這樣的形式是否存在

要確定這樣的形式是否存在於PCRE/PHP、Perl、.NET 或類似的正規表示式中,我們可以使用下列表達式:

問題2:計算這​​樣的編隊

^
(?:
    (?:                   # 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
簡單匹配對於直接匹配並要求計數為匹配數,由於lookbehind支持有限,這個問題無法在PCRE或Perl中直接解。 長度/間接解但是,如果答案被接受為 a 的長度匹配或替換,則可以用以下表達式來回答:

以上是您可以使用正規表示式在 ASCII 影像中找到由三個'X”組成的垂直線嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn