Dalam imej ASCII seperti:
....X....... ..X..X...X.... X.X...X..X..... X....XXXXXX..... X..XXX........... .....X.......... ..............X ..X...........X.... ..X...........X....X... ....X.....
kami' d suka mencari pembentukan garis menegak mudah tiga Xs tanpa menggunakan kaedah terbaik selain daripada regex.
Untuk menentukan sama ada pembentukan sedemikian wujud dalam PCRE/ PHP, Perl, .NET atau regex yang serupa, kita boleh menggunakan ungkapan berikut:
(?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
Padanan Biasa
Untuk pemadanan langsung dan memerlukan kiraan sebagai bilangan padanan, soalan ini tidak boleh diselesaikan secara langsung dalam PCRE atau Perl kerana sokongan pandang belakang yang terhad.
Length/Indirect Solution
Walau bagaimanapun, jika jawapan diterima sebagai panjang perlawanan atau penggantian, maka ia boleh dijawab dengan ungkapan berikut:
^ (?: (?: # 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
Atas ialah kandungan terperinci Bolehkah Anda Cari Garis Menegak Tiga 'X' dalam Imej ASCII Menggunakan Regex?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!