You might not be able to fully solve this problem with regex. There are too many possibilities of other characters between the words...
But you can try this regex:
((?:S+s*){0,5}S*inmateS*(?:s*S+){0,5})
See here : rubular
You might also want to exclude certain characters as they are not counted as words. Right now the regex counts any sequence of non space characters that are surrounded by spaces as word.
To match only real words:
((?:w+s*){0,5}<search word>(?:s*w+){0,5})
But here any non word character (,". etc.) brakes the matching.
So you can go on...
((?:[w"',.-]+s*){0,5}["',.-]?<search word>["',.-]?(?:s*[w"',.-]+){0,5})
This would also match 5 words with one of "',.- around your search term.
To use it in php:
$sourcestring="For example, if a user enters "inmate" as a search word and the MySQL";
preg_match_all('/(?:S+s*){0,5}S*inmateS*(?:s*S+){0,5}/s',$sourcestring,$matches);
echo $matches[0][0]; // you might have more matches, they will be in $matches[0][x]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…