Php 用正则表达式找到相反的结果
我有这个代码,它工作得很好Php 用正则表达式找到相反的结果,php,regex,Php,Regex,我有这个代码,它工作得很好 $match = false; $string = 'The thing is S25697 and blue'; $reg_exp = '^The thing is.*(S\d).*(blue|red)'; $match = @preg_match("/$reg_exp/iu", $string); if ($match) echo "found"; else echo "not found"; 这个正则
$match = false;
$string = 'The thing is S25697 and blue';
$reg_exp = '^The thing is.*(S\d).*(blue|red)';
$match = @preg_match("/$reg_exp/iu", $string);
if ($match) echo "found";
else echo "not found";
这个正则表达式给出了以下结果
$string = 'The thing is S25697 and blue'; #<--$match = true
$string = 'The thing is S2w697 and green'; #<--$match = false
$string = 'The thing is S2e697 and yellow'; #<--$match = false
$string = 'The thing is S2b697 and pink'; #<--$match = false
$string = 'The thing is S256b7 and red'; #<--$match = true
但结果是“发现”了使用
^The thing is.*\b(S\d)(?!.*\b(?:blue|red)\b)
看
解释
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
The thing is 'The thing is'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
S 'S'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
blue 'blue'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
red 'red'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
使用
看
解释
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
The thing is 'The thing is'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
S 'S'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
blue 'blue'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
red 'red'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
if NOT match
if(!$match)
有什么问题吗?你想实现什么?从字符串中提取S\d
,并匹配末尾的颜色?那么,您是否正在尝试检索S2w697
和green
?还是只拿整根绳子?不清楚。在任何情况下,您都可以将(*SKIP)(*FAIL)|+
附加到正则表达式的末尾,以获取与当前模式不匹配的内容。if$match在代码中是固定的。这是无法改变的。我有一个包含1000个条目的文件。从这里,我搜索所有条目,就像在我的正则表达式中一样(S\d是因为这部分在这两者之间总是不同的)。我也总是有不同的颜色。对于第二个正则表达式,我现在正在寻找与第一个正则表达式相同的值,但没有这两个值colors@Koda因此,为了澄清,您希望提取S2w697
和绿色
?所有的字符串都以颜色结尾吗?我只想知道我的字符串的结构是否像正则表达式一样,在第二次运行时,我想知道哪些字符串的结构完全是那样的,但不包含这两种颜色。“^问题是。*(S\d)。*”应始终选中。你在我的第一篇文章中看到了我的两个目标的示例(很抱歉我的英语不好)。如果不匹配if(!$match)
,有什么问题吗?你想实现什么?从字符串中提取S\d
,并匹配末尾的颜色?那么,您是否正在尝试检索S2w697
和green
?还是只拿整根绳子?不清楚。在任何情况下,您都可以将(*SKIP)(*FAIL)|+
附加到正则表达式的末尾,以获取与当前模式不匹配的内容。if$match在代码中是固定的。这是无法改变的。我有一个包含1000个条目的文件。从这里,我搜索所有条目,就像在我的正则表达式中一样(S\d是因为这部分在这两者之间总是不同的)。我也总是有不同的颜色。对于第二个正则表达式,我现在正在寻找与第一个正则表达式相同的值,但没有这两个值colors@Koda因此,为了澄清,您希望提取S2w697
和绿色
?所有的字符串都以颜色结尾吗?我只想知道我的字符串的结构是否像正则表达式一样,在第二次运行时,我想知道哪些字符串的结构完全是那样的,但不包含这两种颜色。“^问题是。*(S\d)。*”应始终选中。你可以在我的第一篇文章中看到我的两个目标示例(很抱歉我的英语不好)
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
The thing is 'The thing is'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
S 'S'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
blue 'blue'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
red 'red'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead