[英]Regex Look Ahead
今天,在一个项目中,我试图使用正则表达式,并了解了组以及如何使用它们。 我正在使用此网站进行测试。问题在于,每当我编写以下正则表达式时:
(?= \\ S * \\ d)
,该网站给了我一个错误: the expression can match 0 characters and therefore can match infinitely.
虽然这不会引发任何错误:
(?= \\ S * \\ d)(\\ S {6,16})
谁能向我解释错误的含义。
因为向前看是断言,并且不消耗任何字符。
(?=\S*\d)
当您以这种方式编写正则表达式时,它将检查它是否包含零个或多个非空格,后跟一个数字。 但是正则表达式引擎不会使用这些字符。 并且指针保持在相同位置。
例
hello123
|
This is the initial position of pointer. It the checking starts from here
hello123
|
(?=\S*\d). Here it matches \S
hello123
|
(?=\S*\d)
This continues till
hello123
|
(?=\S*\d) Now the assertion is matched. The pointer backtracks to the position from where it started looking for regex.
hello123
|
Now you have no more pattern to match. For the second version of the regex, the matching then begins from this postion
那和有什么区别
(?=\S*\d)(\S{6,16})
这里,
(?=\\S*\\d)
此部分进行检查。 我再重复一遍,这部分不消耗任何字符,它只是检查。
(\\S{6,16})
这部分处理输入字符串中的字符。 也就是说,它至少消耗6
非空格字符,最多16
字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.