I have some code in php I made using preg_grep for matching several words in any order that can exist in any context. I'm trying to convert it to java but i can't seem to figure it out.
My php code for converting a keyword to a regex string is:
function createRegexSearch($keywords)
{
$regex = '';
foreach ($keywords as $key)
$regex .= '(?=.*' . $key . ')';
return '/^' . $regex . '/i';
}
It would create a regex string similar to: /^(?=.*bot)/i - which should match robot, robots, bots etc. The same regex string doesn't seem to work in java which is leaving me confused. Currently in java I created a similar effect with contains but would rather use regex.
for (Map.Entry<String, String> entry : mKeyList.entrySet())
{
boolean found = true;
String val = entry.getValue().toLowerCase();
for (int i = 0; i < keywords.length; i++)
{
if (!val.contains(keywords[i].toLowerCase()))
found = false;
}
if (found)
ret.add(entry.getValue());
}