我正在尝试编码搜索功能以搜索字符串并找到单词
就像php + mysql一样
Select * From Column Where ID Like %Word%
我试图用Preg match all和regex
编写相同的想法这是我的代码
$strings = '
( monstername 205 "Devil Troop of Desire")
( monstername 206 " Devil Troop of Pain " )
( monstername 207 "Devil Troop of Greed")
( monstername 208 " Devil Troop of Jealousy ")
( monstername 207 "Mask Troop of Greed" )';
preg_match_all('/monstername\s*(.*?)\s*\"\s*\\b(Jealousy)\b\s*\"\s*\)/i', $strings, $matches, PREG_SET_ORDER);
foreach ($matches as $match){list (, $MonsterNumber) = $match;
echo "$MonsterNumber";
}
输出应为
208
但它不显示输出正确
当我用嫉妒的恶魔队伍取代嫉妒时显示它
我只是想对php + mysql做同样的想法
ID Like%Word%
没有给完整的字符串来查找怪物的数量
答案 0 :(得分:0)
部分"\s*\\b(Jea
不允许除引号和单词开头之间的空格之外的其他字符。你可能想要它们之间的任何字符(甚至没有?),比如".*\\b(Jea
和单词之后的相同问题。
答案 1 :(得分:0)
你必须更好地定义你要搜索的内容:在嫉妒之前和之后你可能有空格/非空格,非引号字符。
preg_match_all('/\( monstername\s*(\d+)\s*"\s*([^"]*\bJealousy\b[^"]*)\s*"\s*\)/i',
$strings, $matches, PREG_SET_ORDER);
或者在搜索脚本中使用
preg_match_all('/\( monstername\s*(\d+)\s*"\s*([^"]*(Jealousy)[^"]*)\s*"\s*\)/i',
$strings, $matches, PREG_SET_ORDER);
对于每个匹配的怪物,它将返回匹配的行,怪物编号,怪物名称和匹配的模式:
Array
(
[0] => Array
(
[0] => ( monstername 208 " Devil Troop of Jealousy ")
[1] => 208
[2] => Devil Troop of Jealousy
[3] => Jealousy
)
)
答案 2 :(得分:0)
如果您只想将怪物编号作为输出,那么这个正则表达式只会匹配数字,而不会因为前瞻和后瞻而与它无关:
/(?<=monstername\s)\d+(?=.*Jealousy.*)/i
但有了这个,你只能在“monstername”和数字之间有一个空白字符。