我有一个字符串,用于描述像这样的n x m个元素的矩阵:
§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);
我需要构建一个正则表达式(或其他东西)来搜索字符串:
$search = "
*F*
FFF
*F*
";
$search = trim($search);
整个网格。在另一方面,我需要找到一个5个不同字母“F”(3个垂直和3个水平)的模式,返回地图上找到的模式的行/列位置。
考虑到输入矩阵可能不同(5x5或10x10或20x25或......),有没有办法用php和正则表达式解决我的问题?
答案 0 :(得分:4)
您可以使用$length=strstr($inputmap,"\n")
查找每行的宽度。然后,您可以构建一个正则表达式,该表达式将找到F,后跟($length-2)
个其他字符,后跟3个F,后跟($length-2)
个其他字符,后跟F。
答案 1 :(得分:3)
你可以这样做(没有正则表达式):
$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
$>
但我必须承认,超大地图可能需要一段时间。
/!\添加一些代码以验证行$vcount + 1/2
实际存在,以及char $hcount + 1
。
答案 2 :(得分:2)
您可以使用以下代码:
$lines = array_filter(preg_split("#\r\n?|\n#", $string)); // Creating array of lines
$matrix = array_map('str_split', $lines); // Creating a matrix
foreach($lines as $line_number => $line_content){ // Looping through the lines
$pos = strpos($line_content, 'FFF');
if(!$pos === false){// If FFF found
while(true){
if(isset($matrix[$line_number-1][$pos+1],$matrix[$line_number+1][$pos+1]) && $matrix[$line_number-1][$pos+1] == 'F' && $matrix[$line_number+1][$pos+1] == 'F'){ //Checking ...
echo 'Found at: X:'.$pos.' & Y:'.$line_number.'<br>'; // Ouput
}
$pos = strpos($line_content, 'FFF', $pos+1); // Search further
if(!is_int($pos)){
break;
}
}
}
}
发生了什么事?
FFF
这是为了提高性能。因此,我们不是循环遍历整个矩阵并搜索F
,而是直接搜索FFF
F
<强> Online demo 强>
请注意,坐标是+ 的中心
答案 3 :(得分:1)
尝试这种模式:
如果行尾为\n
:
$pattern = '~F.{'. ($n-2) . '}FFF.{' . ($n-2) . 'F~s';
如果行尾为\r\n
,则替换为$n-3
或者更好地使用quinxorin技巧来知道\n
答案 4 :(得分:1)
@FrankieTheKneeMan&amp;所有......我最初的方法是为输入网格图的'n'字符构建'n'正则表达式。有点像...
$search_001 = "
.F......................................
FFF.....................................
.F......................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";
$search_002 = "
..F.....................................
.FFF....................................
..F.....................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";
(... omissis ...)
$search_100 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
..........F.............................
.........FFF............................
..........F.............................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";
(... omissis ...)
$search_999 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
......................................F.
.....................................FFF
......................................F.
";
其中“。” obvioulsy意味着任何角色。
这是一个愚蠢的想法吗?!?