Php regex find substring in substring

I am still playing for one project with the corresponding words.

Suppose I have a given string, for example maxmuster . Then I want to mark this part of my random word maxs , which are in maxmuster in the correct order, for example, letters.

I will give some examples, and then I will tell what I have already done. Let's save the maxmuster line. The bold part is regex-consistent (best would be in php, but could be python, bash, javascript, ...)

max s

My maxmu is

Le mu

anxiety

Of course there will also be m , u , .... I know this, I will fix it later. However, the solution, however, should not be so difficult, so I try to split the word in groups as follows:

/(maxmuster)?|(maxmuste)?|(maxmust)?|(maxmus)?|(maxmu)?|(maxm)?|(max)?|(ma)?|(m)?/gui 

But then I forgot, of course, other combinations, for example:

(axmuster)(xmus) and so on. Did I really have to do this or was there a simple regular expression trick to solve this issue, as I said above?

Thank you very much

+5
source share
3 answers

It looks like you need line intersection. If you don't mind an idea without regular expression, check out Wikibooks Algorithm Implementation / Strings / Longest Common Substring PHP Section.

 foreach(["maxs", "Mymaxmuis", "Lemu", "muster"] AS $str) echo get_longest_common_subsequence($str, "maxmuster") . "\n"; 

tah
maxmu
mu
to collect

Check out this demo at eval.in (no-use comparison).

+2
source

TL DR

Using regular expressions:

 longestSubstring(['Mymaxmuis', 'axmuis', 'muster'], buildRegexFrom('maxmuster')); 

Full fragment


Using the regex below, you can match all the true substrings of the maxmuster string:

 (?|((?: m(?=a) |(?<=m)a |a(?=x) |(?<=a)x |x(?=m) |(?<=x)m |m(?=u) |(?<=m)u |u(?=s) |(?<=u)s |s(?=t) |(?<=s)t |t(?=e) |(?<=t)e |e(?=r) |(?<=e)r )+)|([maxmuster])) 

Live demo

You should prepare such a regular expression from a word like maxmuster so that you need to call its function:

 function buildRegexFrom(string $word): string { // Split word to letters $letters = str_split($word); // Creating all side of alternations in our regex foreach ($letters as $key => $letter) if (end($letters) != $letter) $regex[] = "$letter(?={$letters[$key + 1]})|(?<=$letter){$letters[$key + 1]}"; // Return whole cooked pattern return "~(?|((?>".implode('|', $regex).")+)|([$word]))~i"; } 

To return the longest match, you need to sort the results according to the length of the matches from the longest to the shortest. This means that you need to write another piece of code for it:

 function longestSubstring(array $array, string $regex): array { foreach ($array as $value) { preg_match_all($regex, $value, $matches); usort($matches[1], function($a, $b) { return strlen($b) <=> strlen($a); }); // Store longest match being sorted $substrings[] = $matches[1][0]; } return $substrings; } 

Putting it all together:

 print_r(longestSubstring(['Mymaxmuis', 'axmuis', 'muster'], buildRegexFrom('maxmuster'))); 

Outputs:

 Array ( [0] => maxmu [1] => axmu [2] => muster ) 

Php live demo

+1
source

Here is my problem using regex.

 <?php $subject="maxmuster"; $str="Lemu"; $comb=str_split($subject); // Split into single characters. $len=strlen($subject); for ($i=2; $i<=$len; $i++){ for($start=0; $start<$len; $start++){ $temp=""; $inc=$start; for($j=0; $j<$i; $j++){ $temp=$temp.$subject[$inc]; $inc++; } array_push($comb,$temp); } } echo "Matches are:\n"; for($i=0; $i<sizeof($comb); $i++){ $pattern = "/".$comb[$i]."/"; if(preg_match($pattern,$str, $matches)){ print_r($matches); }; } ?> 

And here is the perfect demonstration .

0
source

Source: https://habr.com/ru/post/1267244/


All Articles