php垂直正则表达式搜索 [英] php vertical regular expression search

查看:73
本文介绍了php垂直正则表达式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串描述了一个由 n x m 元素组成的矩阵,如下所示:

I've a string describing a matrix of n x m elements like this one:

§inputmap = "
~~~~~~~~~~~~~~~~~~~~B~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~BBB........BBB~~~~~~~~~~~~~
~~~~~~~~~~BB...............FBB~~~~~~~~~~
~~~~~~~~BB....................BB~~~~~~~~
~~~~~~BB.....F..................BB~~~~~~
~~~~~BB.....................F.....B~~~~~
~~~~B..............................B~~~~
~~~B........F.......................B~~~
~~BB.........F......................BB~~
~~B................F.................BB~
~BF....F....F........................FB~
~B.....................................B
B.....................................FB
B........F......F......................B
B...........................F..........B
B......................................B
B......................................B
B.......F.......................F......B
B......FFF.............................B
B.......F.............................FB
~B..................F.................FB
~BF...........................F.......B~
~~B...F...........F..........FFFFF.F.BB~
~~BB..................F..F....F.....BB~~
~~~B.......................FF.FF....B~~~
~~~~B..............................B~~~~
~~~~~BB...........................B~~~~~
~~~~~~BB........................BB~~~~~~
~~~~~~~~BB..........F..........B~~~~~~~~
~~~~~~~~~~BB................BB~~~~~~~~~~
~~~~~~~~~~~~~BBB.......F.BBB~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~BBBBBB~~~~~~~~~~~~~~~~~
";
$inputmap = trim($inputmap);

我需要构建一个正则表达式(或其他东西)来搜索字符串:

I need to build a regular expression (or something else) to search the string:

$search = "
*F*
FFF
*F*
";
$search = trim($search);

在整个网格上.另一方面,我需要找到一个由 5 个不同字母F"(垂直 3 个,水平 3 个)组成的模式,以获取在地图上找到的模式的行/列位置.

on the whole grid. On other hands I need to find a pattern of 5 distinct letter "F" (3 vertically and 3 horizzontally) getting back the rows/columns position of the pattern(s) found on the map.

考虑到输入矩阵可能不同(5x5 或 10x10 或 20x25 或 ...),有没有办法解决我的 php 和正则表达式问题?

Considering that the input matrix can be different (5x5 or 10x10 or 20x25 or ...), is there a way to solve my problem with php and regular expressions?

推荐答案

你可以这样做(不用正则表达式):

You can do something like this (without regular expressions) :

$map = explode("\n", $inputmap);

for ($vcount = 0; $vcount < sizeof($map); ++$vcount) {  // Loop through the map vertically
    for ($hcount = 0; $hcount < strlen($map[$vcount]); ++$hcount) { // Loop through each character of each line
        if ($map[$vcount][$hcount] == "F") {
            if ($map[$vcount + 1][$hcount - 1] == "F" && $map[$vcount + 1][$hcount] == "F" && 
                $map[$vcount + 1][$hcount + 1] == "F" && $map[$vcount + 2][$hcount] == "F")
                echo "Pattern found, starting at : (v)$vcount x (h)$hcount";
        }
    }
}

$> php test.php
php test.php
Pattern found, starting at : (v)18 x (h)8
Pattern found, starting at : (v)22 x (h)30
$>

但我必须承认,超大地图可能需要一段时间.

But I must admit that it could take a while for extra-big maps.

/!\ 添加一些代码来验证行 $vcount + 1/2 确实存在,并且字符 $hcount + 1 也是如此.

/!\ add some code to verify that the line $vcount + 1/2 actually exists, and the char $hcount + 1 too.

这篇关于php垂直正则表达式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆