簡體   English   中英

php垂直正則表達式搜索

[英]php vertical regular expression search

我有一個字符串描述像這樣的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);

我需要構建一個正則表達式(或其他東西)來搜索字符串:

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

在整個網格上。 另一方面,我需要找到一個5個不同字母“F”的圖案(垂直3個,水平3個),回到地圖上找到的圖案的行/列位置。

考慮到輸入矩陣可以是不同的(5x5或10x10或20x25或......),有沒有辦法解決我的PHP和正則表達式的問題?

您可以使用$length=strstr($inputmap,"\\n")來查找每一行的寬度。 然后你可以構造一個正則表達式,它會找到一個F,然后是($length-2)其他字符,然后是3個Fs,接着是($length-2)其他字符,然后是F.

你可以這樣做(沒有正則表達式):

$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也是如此。

您可以使用以下代碼:

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

這是怎么回事 ?

  1. 我們創建了一系列線條
  2. 我們創建一個矩陣
  3. 我們遍歷這些並搜索FFF這是為了提高性能。 因此,我們不是循環遍歷整個矩陣並搜索F ,而是直接搜索FFF
  4. 檢查矩陣中是否存在該值(以防止未定義的索引錯誤),然后檢查它是否等於F
  5. 產量
  6. 在同一行上進一步搜索

在線演示

請注意,坐標是+的中心

嘗試這種模式:

如果行尾是\\n

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

如果行尾為\\r\\n則替換為$n-3

或者更好地使用quinxorin技巧來知道直到\\n的行長度

@FrankieTheKneeMan&all ...我最初的方法是為輸入網格圖的'n'字符構建'n'正則表達式。 就像是...

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


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

(......遺漏......)

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

(......遺漏......)

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

在哪里“。” obvioulsy意味着任何性格。

這是一個愚蠢的想法嗎?!?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM