Rumah >pembangunan bahagian belakang >tutorial php >Bolehkah Regex Mengesan dan Mengira Corak 'X' Menegak dalam Seni ASCII?
Dalam imej ASCII seperti di bawah:
....X....... ..X..X...X.... X.X...X..X..... X....XXXXXX..... X..XXX........... .....X.......... ..............X ..X...........X.... ..X...........X....X... ....X.....
Kami menyasarkan untuk mengesan corak berikut:
X X X
di mana tiga X dijajarkan secara menegak.
Ya, regex berikut boleh mengenal pasti kehadiran pembentukan X menegak:
(?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
Demo Dalam Talian: https://regex101.com/r/YxPeXe/1
Penyelesaian Tidak Langsung
Untuk mengira bilangan pembentukan, kita boleh melakukan penggantian berikut:
regex =>
di mana regex ialah corak di atas. Panjang rentetan yang terhasil akan sama dengan kiraan padanan.
Demo Dalam Talian: https://regex101.com/r/Tx6R63/1
Atas ialah kandungan terperinci Bolehkah Regex Mengesan dan Mengira Corak 'X' Menegak dalam Seni ASCII?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!