简体   繁体   English

PHP否定前瞻正则表达式不起作用

[英]PHP negative lookahead regex not working

I am trying to construct a regex in my PHP application that takes a variable, such as $search , and replaces that with another variable. 我试图在我的PHP应用程序中构造一个正则表达式,该正则表达式采用一个变量,例如$search ,并将其替换为另一个变量。 What I am trying to do specifically, is replace the $search value only if it is not followed directly by an = . 我要特别尝试的是仅在$search值不直接跟有=替换它。

What I have tried so far is: 到目前为止,我尝试过的是:

preg_replace("/($search)(?!=)/i", $replacement, $string, -1, $count);

But this has not worked. 但这没有用。 I'm sure I am just misunderstanding something, but I can not seem to determine what that is. 我确定我只是误解了一些东西,但是我似乎无法确定那是什么。 I am fairly new to regex, and I've looked at several of the answers from previous questions and have not been able to figure it out. 我对regex相当陌生,并且已经查看了先前问题中的几个答案,但无法弄清楚。

If you need any other information, let me know. 如果您需要其他任何信息,请告诉我。

EDIT 编辑

To clarify, assume the following code: 为了澄清,假设以下代码:

$searchTerm = "Hello";
$search = "/($searchTerm)(<?!=)/i";
$replacement = '<span style="background:red;">$0</span>';
$result = preg_replace($search, $replacement, $string, -1, $count);

where the value of $string would be something like: $ string的值类似于:

$string = "hello=hola";

So the idea is that if "hello" was not followed by and '=', then it would highlight it in red, but if it is, then nothing happens. 因此,我们的想法是,如果“ hello”后面没有加“ =”,它将以红色突出显示,但是如果没有,则什么也不会发生。 At assume that the value of $string can change. 假设$ string的值可以更改。 Very lame example, I know, but hopefully it clears it up a little. 我知道这是非常la脚的例子,但希望它能使它有所清除。

Actually you need to escape $ character by a backslash \\ in your pattern. 实际上,您需要在模式中使用反斜杠\\来使$字符转义。 Also to make it more confident, you can add \\s* which means zero or more white space(s) before = character. 另外,为了使其更加自信,您可以添加\\s* ,这表示=字符之前有零个或多个空格。

Use this pattern: 使用以下模式:

/\$search(?!\s*\=)/i

Online Demo 在线演示

  • / delimiter /分隔符
  • \\$ matches $ literally \\$ $从字面上匹配$
  • search matches that word literally search从字面上匹配该词
  • (?! negative Lookahead which checks being both white space(s) (if exists) and = after that variable name. (?!否定的Lookahead,它检查在该变量名之后是否同时包含空格(如果存在)=
  • /i modifire makes the pattern insensitive (there isn't any different between az and AZ ) /i modifire使模式不敏感azAZ之间没有任何区别)

Note: You have to use single quotes '/pattern/' for pattern above. 注意:上面的'/pattern/'必须使用单引号'/pattern/' If you want to use double quotes " then you should escape backslashes again: 如果要使用双引号" ,则应再次转义反斜杠:

/\\$search(?!\\s*\\=)/i

The $ is an anchor in regex to match the end of a line, so since it's a special character you have to escape it. $是正则表达式中的锚点,用于匹配行尾,因此,由于它是一个特殊字符,因此必须对其进行转义。

You can use: 您可以使用:

preg_replace("/(\\$search)(?!=)/i", $replacement, $string, -1, $count);

or 要么

preg_replace('/(\$search)(?!=)/i', $replacement, $string, -1, $count);

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM