正则表达式(?

时间:2022-05-11 15:45:09

I'm trying to understand a piece of code and came across this regular expression used in PHP's preg_replace function.

我正在尝试理解一段代码并遇到了PHP的preg_replace函数中使用的这个正则表达式。

'/(?<!-)color[^{:]*:[^{#]*$/i'

This bit... (?<!-) doesnt appear in any of my reg-exp manuals. Anyone know what this means please? (Google doesnt return anything - I dont think symbols work in google.)

这个位......(?<! - )没有出现在我的任何reg-exp手册中。有人知道这意味着什么吗? (谷歌不会返回任何东西 - 我不认为符号在谷歌工作。)

3 个解决方案

#1


10  

The ?<! at the start of a parenthetical group is a negative lookbehind. It asserts that the word color (strictly, the c in the engine) was not preceded by a - character.

?<!在一个括号组的开头是负面的背后。它声称颜色一词(严格地说,引擎中的c)前面没有 - 字符。

So, for a more concrete example, it would match color in the strings:

因此,对于更具体的示例,它将匹配字符串中的颜色:

color
+color
someTextColor

But it will fail on something like -color or background-color. Also note that the engine will not technically "match" whatever precedes the c, it simply asserts that it is not a hyphen. This can be an important distinction depending on the context (illustrated on Rubular with a trivial example; note that only the b in the last string is matched, not the preceding letter).

但它会失败,如颜色或背景颜色。还要注意,引擎在技术上不会匹配c之前的任何内容,它只是断言它不是连字符。这可能是一个重要的区别,取决于上下文(在Rubular上有一个简单的例子;请注意,只有最后一个字符串中的b匹配,而不是前面的字母)。

#2


6  

PHP uses perl compatible regular expressions (PCRE) for the preg_* functions. From perldoc perlre:

PHP使用perl兼容的正则表达式(PCRE)作为preg_ *函数。来自perldoc perlre:

"(?<!pattern)"
A zero-width negative look-behind assertion. For example
"/(?<!bar)foo/" matches any occurrence of "foo" that does
not follow "bar". Works only for fixed-width look-
behind.

“(?<!pattern)”零宽度负面后瞻断言。例如“/(?<!bar)foo /”匹配任何不遵循“bar”的“foo”。仅适用于固定宽度的后视镜。

#3


4  

I'm learning regular expressions using Python's re module!

我正在使用Python的re模块学习正则表达式!

http://docs.python.org/library/re.html

http://docs.python.org/library/re.html

Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

匹配如果字符串中的当前位置前面没有匹配....这称为负后观断言。与正向lookbehind断言类似,包含的模式必须仅匹配某些固定长度的字符串。以负反向断言开始的模式可以在被搜索的字符串的开头匹配。

#1


10  

The ?<! at the start of a parenthetical group is a negative lookbehind. It asserts that the word color (strictly, the c in the engine) was not preceded by a - character.

?<!在一个括号组的开头是负面的背后。它声称颜色一词(严格地说,引擎中的c)前面没有 - 字符。

So, for a more concrete example, it would match color in the strings:

因此,对于更具体的示例,它将匹配字符串中的颜色:

color
+color
someTextColor

But it will fail on something like -color or background-color. Also note that the engine will not technically "match" whatever precedes the c, it simply asserts that it is not a hyphen. This can be an important distinction depending on the context (illustrated on Rubular with a trivial example; note that only the b in the last string is matched, not the preceding letter).

但它会失败,如颜色或背景颜色。还要注意,引擎在技术上不会匹配c之前的任何内容,它只是断言它不是连字符。这可能是一个重要的区别,取决于上下文(在Rubular上有一个简单的例子;请注意,只有最后一个字符串中的b匹配,而不是前面的字母)。

#2


6  

PHP uses perl compatible regular expressions (PCRE) for the preg_* functions. From perldoc perlre:

PHP使用perl兼容的正则表达式(PCRE)作为preg_ *函数。来自perldoc perlre:

"(?<!pattern)"
A zero-width negative look-behind assertion. For example
"/(?<!bar)foo/" matches any occurrence of "foo" that does
not follow "bar". Works only for fixed-width look-
behind.

“(?<!pattern)”零宽度负面后瞻断言。例如“/(?<!bar)foo /”匹配任何不遵循“bar”的“foo”。仅适用于固定宽度的后视镜。

#3


4  

I'm learning regular expressions using Python's re module!

我正在使用Python的re模块学习正则表达式!

http://docs.python.org/library/re.html

http://docs.python.org/library/re.html

Matches if the current position in the string is not preceded by a match for .... This is called a negative lookbehind assertion. Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length. Patterns which start with negative lookbehind assertions may match at the beginning of the string being searched.

匹配如果字符串中的当前位置前面没有匹配....这称为负后观断言。与正向lookbehind断言类似,包含的模式必须仅匹配某些固定长度的字符串。以负反向断言开始的模式可以在被搜索的字符串的开头匹配。