This behaves better, in that it works for the whole of both strings. It could
still be optimized. The array that gets returned has all (afaict) the matches.
$str1:="This is my dog"
$str2:="My dog does not have fleas"
//$str2:="My dog does not have fleas"
//$str1:="The quick brown fox jumps over the lazy dog"
//$str2:="The quick black fox leaps over the laziest dog"
//$str1:="The quick brownbrown fox jumps over the lazy dog"
//$str2:="fox leaps over the laziest dog The quick brownbrown"
ARRAY TEXT($aMatch;0)
getCommon2 ($str1;$str2;->$aMatch)
SORT ARRAY($aMatch;<)
// ----------------------------------------------------
// Method: getCommon2
// -
// INPUT1: Text
// INPUT2: Text
// INPUT3: Pointer - to text array
// OUTPUT:
// ----------------------------------------------------
$str1:=$1
$str2:=$2
$unique:=True
$sepChar:=Char(9)
If (Length($str1)>Length($str2))
$longer:=$str1
$shorter:=$str2
Else
$longer:=$str2
$shorter:=$str1
End if
$shortLen:=Length($shorter)
$padStr:=$sepChar*$shortLen
$shorter:=$shorter
$longer:=$padStr+$longer
$longLen:=Length($longer)
$maxLen:=$shortLen*2+$longLen
$loop:=1
ARRAY TEXT($aMatch;0)
Repeat
Case of
: ($loop<=$shortLen) // starting
$pos1:=$shortLen
$pos2:=$shortLen+$loop
: ($loop>=$shortLen) & (($shortLen+$loop)<=$longLen)
$pos1:=$loop
$pos2:=$shortLen+$loop
Else
$pos1:=$loop
$pos2:=$longLen
End case
$shorter:=$sepChar+$shorter // push str1 onto str2
$str3:=$sepChar*($pos2) // longer than needed?
For ($i;$pos1;$pos2)
For ($j;$pos1;$i)
If ($shorter[[$j]]=$longer[[$j]])
$str3[[$j]]:=$shorter[[$j]]
End if
End for
ARRAY TEXT($aTemp;0)
Parse_TextToArray ($str3;->$aTemp;$sepChar;$unique) // as if
GET TEXT KEYWORDS let you pick the separator
End for
$size:=Size of array($aTemp)
For ($m;1;$size)
If (Find in array($aMatch;$aTemp{$m})=-1)
APPEND TO ARRAY($aMatch;$aTemp{$m})
End if
End for
$loop:=$loop+1
Until ($pos1>$longLen)
COPY ARRAY($aMatch;$3->)
Keith - CDI
> On Aug 22, 2019, at 3:12 PM, Chip Scheide via 4D_Tech <[email protected]>
> wrote:
>
> Given 2 strings,
> I want to find, and return, the longest substring which is the same in
> both, regardless where in either string the longest substring starts.
>
> ex:
> 1- This is my dog
> 2- My dog does not have fleas
> longest common string is 'my dog'
>
> how to go about this, efficiently?
> I am assuming that there is regex black magic that would do this.
>
**********************************************************************
4D Internet Users Group (4D iNUG)
Archive: http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************