Home > Backend Development > PHP Tutorial > Can a Regular Expression Detect Three Xs in a Vertical Line Formation in an ASCII Image?

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

Barbara Streisand
Release: 2024-11-08 02:47:01
Original
458 people have browsed it

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

Determine if a Vertical Line Formation of Three Xs Exists

Using modern regex flavors like PCRE/PHP, Perl, .NET, or similar, it is possible to determine if a vertical line formation of three Xs exists in an ASCII "image."

Consider the following regex expression:

(?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
Copy after login

This expression employs lookaheads with self-referencing capturing groups to count the number of characters on each line before an X. It attempts to match three X characters in a vertical formation by matching the conditions specified in the lookaheads.

Online Demo: https://regex101.com/r/Xb5FXl/2

Count the Number of Vertical Line Formations of Three Xs

While it's not directly solvable in regex flavors with limited lookbehinds like Perl and PCRE, it is possible to indirectly determine the count by manipulating the matched string.

The following solution modifies m.buettner's "partial PCRE solution" by matching and counting the number of characters in the portion of the match where the three Xs occur.

^
(?:
    (?:                   # 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
Copy after login

This expression matches the same formations as the previous one, but it adds a third capturing group and counts the length of the matched characters within that group. By substituting the matches with just the contents of the third capturing group, the resulting string length represents the number of three-X formations.

Example Usage in Perl:

$in =~ s/regex//gmx;
$count = length $in;
Copy after login

Online Demo: https://regex101.com/r/iqxY1a/1

Test Cases:

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)
Copy after login

The above is the detailed content of Can a Regular Expression Detect Three Xs in a Vertical Line Formation in an ASCII Image?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template