php-regex精确匹配
我有以下字符串:php-regex精确匹配,php,regex,Php,Regex,我有以下字符串: Falchion-Case P90-ASH-WOOD-WELL-WORN 我还有以下URL,它们位于文本文件中: http://csgo.steamanalyst.com/id/115714004/FALCHION-CASE-KEY- http://csgo.steamanalyst.com/id/115716486/FALCHION-CASE- http://csgo.steamanalyst.com/id/2018/P90-ASH-WOOD-WELL-WORN 我将遍历
Falchion-Case
P90-ASH-WOOD-WELL-WORN
我还有以下URL,它们位于文本文件中:
http://csgo.steamanalyst.com/id/115714004/FALCHION-CASE-KEY-
http://csgo.steamanalyst.com/id/115716486/FALCHION-CASE-
http://csgo.steamanalyst.com/id/2018/P90-ASH-WOOD-WELL-WORN
我将遍历文本文件中的每一行,并检查是否在URL中找到该字符串:
// Read from file
if (stristr($item, "stattrak") === FALSE) {
$lines = file(public_path().'/csgoanalyst.txt');
foreach ($lines as $line) {
// Check if the line contains the string we're looking for, and print if it does
if(preg_match('/(?<!\-)('.$item.')\b/',$line) != false) { // case insensitive
echo $line;
break;
}
}
}
//从文件中读取
if(stristr($item,“stattrak”)==FALSE){
$lines=file(public_path()。/csgoanalyst.txt');
foreach($line作为$line){
//检查行是否包含我们要查找的字符串,如果包含,则打印
如果(preg_)匹配('/(?)?http://csgo.steamanalyst.com/id/115716486/FALCHION-CASE-
是有效的您还可以使用否定的前瞻来否定不需要的情况:
preg_match('/(?<!-)'. preg_quote($item) .'(?!-\w)/i', $line);
preg_match('/(?假设行结束,请尝试修改您的regx以匹配行尾
'/('.$item.')$/'
这会匹配的
http://csgo.steamanalyst.com/id/115714004/FALCHION-CASE-KEY- <<end of line
要可选地匹配一个结束连字符,请使用线尾的$
,两个URL中都会出现$item
。-
字符是否有任何额外的含义?@Bart Friederichs no-
只是字符串的一部分,我正在处理的另外3000个字符串中也有该字符,而不是其他字符串。如果添加-代码>到项目的结尾它将$item
不包括尾随的-
如果您不想要精确匹配,那么您也可以添加-?
到项目的结尾?
表示仍然匹配:http://csgo.steamanalyst.com/id/115714004/FALCHION-CASE-KEY-
我在找:http://csgo.steamanalyst.com/id/115714004/FALCHION-CASE-
我怀疑是否匹配,除非您在那里有错误的$item
,而我无法找到与此匹配的项
'/('.$item.')\-?$/'