Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php vertical regular expression search

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

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.

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?

like image 401
Stefano Radaelli Avatar asked Jun 10 '13 14:06

Stefano Radaelli


People also ask

Which function is used for search regular expression for validation purpose in PHP?

The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise. The preg_match_all() function matches all occurrences of pattern in string.

How do I match a string in PHP?

The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().

What are PHP metacharacters?

The . The dot . metacharacter matches every character in a string except newlines. The asterisk metacharacter * indicates that the character that precedes it in the regular expression is repeated zero or more times.

What will search for a in a regular expression?

A regular expression is a form of advanced searching that looks for specific patterns, as opposed to certain terms and phrases. With RegEx you can use pattern matching to search for particular strings of characters rather than constructing multiple, literal search queries.


2 Answers

You can use $length=strstr($inputmap,"\n") to find the width of each line. 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.

like image 108
IanPudney Avatar answered Nov 11 '22 19:11

IanPudney


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.

like image 3
B F Avatar answered Nov 11 '22 19:11

B F