简体   繁体   English

php垂直正则表达式搜索

[英]php vertical regular expression search

I've a string describing a matrix of nxm elements like this one: 我有一个字符串描述像这样的nxm元素矩阵:

§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);

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. 另一方面,我需要找到一个5个不同字母“F”的图案(垂直3个,水平3个),回到地图上找到的图案的行/列位置。

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? 考虑到输入矩阵可以是不同的(5x5或10x10或20x25或......),有没有办法解决我的PHP和正则表达式的问题?

You can use $length=strstr($inputmap,"\\n") to find the width of each line. 您可以使用$length=strstr($inputmap,"\\n")来查找每一行的宽度。 You can then construct a regular expression that will find an F, followed by ($length-2) other characters, followed by 3 Fs, followed by ($length-2) other characters, followed by an F. 然后你可以构造一个正则表达式,它会找到一个F,然后是($length-2)其他字符,然后是3个Fs,接着是($length-2)其他字符,然后是F.

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. 但我必须承认,超大地图可能需要一段时间。

/!\\ add some code to verify that the line $vcount + 1/2 actually exists, and the char $hcount + 1 too. /!\\添加一些代码来验证行$vcount + 1/2实际存在,而char $hcount + 1也是如此。

You could use the following code: 您可以使用以下代码:

$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;
            }
        }
    }
}

What's going on ? 这是怎么回事 ?

  1. We create an array of lines 我们创建了一系列线条
  2. We create a matrix 我们创建一个矩阵
  3. We loop through the lines and search for FFF this is to increase performance. 我们遍历这些线并搜索FFF这是为了提高性能。 So instead of looping through the whole matrix and searching for F , we directly search for FFF 因此,我们不是循环遍历整个矩阵并搜索F ,而是直接搜索FFF
  4. Check if the value exists in the matrix (to prevent undefined index errors) and then check if it equals F 检查矩阵中是否存在该值(以防止未定义的索引错误),然后检查它是否等于F
  5. Output 产量
  6. Search further on the same line 在同一行上进一步搜索

Online demo 在线演示

Note that the coordinates are the center of the + 请注意,坐标是+的中心

try with this pattern: 尝试这种模式:

if the end of line is \\n : 如果行尾是\\n

$pattern = '~F.{'. ($n-2) . '}FFF.{' . ($n-2) . 'F~s';

if the end of line is \\r\\n then replace by $n-3 如果行尾为\\r\\n则替换为$n-3

or better use quinxorin trick to know the line length until the \\n 或者更好地使用quinxorin技巧来知道直到\\n的行长度

@FrankieTheKneeMan & all... my initial approach was to build 'n' regular expression for 'n' chars of the input grid map. @FrankieTheKneeMan&all ...我最初的方法是为输入网格图的'n'字符构建'n'正则表达式。 Something like... 就像是...

$search_001 = "
.F......................................
FFF.....................................
.F......................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";


$search_002 = "
..F.....................................
.FFF....................................
..F.....................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";

(... omissis ...) (......遗漏......)

$search_100 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
..........F.............................
.........FFF............................
..........F.............................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
";

(... omissis ...) (......遗漏......)

$search_999 = "
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
......................................F.
.....................................FFF
......................................F.
";

where "." 在哪里“。” obvioulsy means any character. obvioulsy意味着任何性格。

Is this a stupid idea?!? 这是一个愚蠢的想法吗?!?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM