正则表达式是什么意思?

时间:2022-02-09 01:58:53

May I know what ?= means in a regular expression? For example, what is its significance in this expression:

我可以知道什么意思吗?例如,这个表达的意义是什么?

(?=.*\d).

3 个解决方案

#1


36  

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured.

是一个正向的展望,一种零宽度的断言。它的意思是捕获的匹配必须后面跟着括号内的任何东西,但是那个部分没有被捕获。

Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

您的示例意味着匹配之后需要0个或多个字符,然后是一个数字(但同样地,这个部分没有被捕获)。

#2


0  

The below expression will find the last number set in a filename before its extension (excluding dot (.)).

下面的表达式将找到扩展之前文件名中的最后一个数字集(不包括点(.))。

'\d+(?=\.\w+$)'

file4.txt will match 4.

file4。三种匹配4。

file123.txt will match 123.

file123。txt将匹配123。

demo.3.js will match 3 and so on.

demo.3。js将匹配3,以此类推。

#3


-1  

(?=pattern) is a zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.

(?=pattern)是一个零宽度的正向前视断言。例如,/\w+(?=\t)/匹配一个单词后跟一个标签,而不包含$&中的标签。

#1


36  

?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured.

是一个正向的展望,一种零宽度的断言。它的意思是捕获的匹配必须后面跟着括号内的任何东西,但是那个部分没有被捕获。

Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

您的示例意味着匹配之后需要0个或多个字符,然后是一个数字(但同样地,这个部分没有被捕获)。

#2


0  

The below expression will find the last number set in a filename before its extension (excluding dot (.)).

下面的表达式将找到扩展之前文件名中的最后一个数字集(不包括点(.))。

'\d+(?=\.\w+$)'

file4.txt will match 4.

file4。三种匹配4。

file123.txt will match 123.

file123。txt将匹配123。

demo.3.js will match 3 and so on.

demo.3。js将匹配3,以此类推。

#3


-1  

(?=pattern) is a zero-width positive lookahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $&.

(?=pattern)是一个零宽度的正向前视断言。例如,/\w+(?=\t)/匹配一个单词后跟一个标签,而不包含$&中的标签。