首頁 > 後端開發 > php教程 > 正規表示式能否偵測 ASCII 影像中垂直線結構中的三個 X?

正規表示式能否偵測 ASCII 影像中垂直線結構中的三個 X?

Barbara Streisand
發布: 2024-11-08 02:47:01
原創
442 人瀏覽過

Can a Regular Expression Detect Three Xs in a Vertical Line Formation in an ASCII Image?

確定是否存在三個X 的垂直線形式

使用現代正規表示式風格,如PCRE/PHP、Perl、.NET 或類似的,可以確定如果ASCII「影像」中存在由三個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
登入後複製

此表達式使用具有自引用捕獲組的前瞻來計算X 之前每行的字元數。它嘗試透過匹配前瞻中指定的條件來匹配垂直格式中的三個 X 字元。

線上示範: https://regex101 .com/r/Xb5l/2

計算三個X 的垂直線形成的數量

雖然它不能在像Perl 和PCRE 這樣的有限後向的正則表達式風格中直接解決,但可以透過操作匹配的字串來間接確定計數。

以下解決方案透過匹配和計算匹配中出現三個 X 的部分中的字元數來修改 m.buettner 的「部分 PCRE 解決方案」。

^
(?:
    (?:                   # 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
登入後複製

此表達式與前一個表達式匹配相同的格式,但它添加了第三個捕獲組併計算該組中匹配字符的長度。透過僅用第三個捕獲組的內容替換匹配項,產生的字串長度表示三 X 形式的數量。

Perl 中的範例用法:

$in =~ s/regex//gmx;
$count = length $in;
登入後複製

線上示範: https://regex101.com/r/iqxY1a/1

測試案例:

Test #0:
--------------------
X
X
X

result: 1 (X)


Test #1:
--------------------
..X....
..X....
..X....

result: 1 (.)


Test #2:
--------------------
..X.X..
..X.X..
....X..

result: 1 (.)


Test #3:
--------------------
..X....
..X....
...X...

result: 0 ()


Test #4:
--------------------
..X....
...X...
..X....

result: 0 ()


Test #5:
--------------------
....X..
.X..X..
.X.....

result: 0 ()


Test #6:
--------------------
.X..X..
.X.X...
.X.X...

result: 1 (.)


Test #7:
--------------------
.X..X..
.X..X..
.X..X..

result: 2 (.X)


Test #8:
--------------------
XXX
XXX
XXX

result: 3 (XXX)


Test #9:
--------------------
X.X.X
XXXXX
XXXXX
.X.X.

result: 5 (XXXXX)


Test #10:
--------------------
1....X.......
2..X..X...X....
3X.X...X..X.....
4X....XXXXXX.....
5X..XXX...........
6.....X..........
7.........X....X
8..X......X....X....
9..X......X....X....X...
A....X.....
B.X..X..
C.....
XXX
XXX
XXX
.

result: 8 (3458.XXX)
登入後複製

以上是正規表示式能否偵測 ASCII 影像中垂直線結構中的三個 X?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板