开发者

Regular Expression: Something out of nothing?

So * by itself means repeat the previous item zero or more times. The output of * is nothing. What about **? This gives an output, but how does matching zero or more times of nothin开发者_如何转开发g give something? Could you also explain that please? Same for ?*: Nothing precedes ?, so that is nothing right? How does matching zero or more times of nothing give something?

mugbear:~# grep '*' emptyspace                                                  
mugbear:~# grep '**' emptyspace                                                 
line1
line2

line4
line5

line7
mugbear:~# grep '?' emptyspace
mugbear:~# grep '?*' emptyspace                                         
line1
line2

line4
line5

line7


A leading * is generally not magic because of its context

You are asking questions with answers that are not fully specified and as such are almost certain to depend on the specific RE implementation.

For that matter, there isn't even anything close to a single standard RE, and the variations are not slightly different interpretations but dramatically different pattern definitions.

At first, there was classic grep / sed / ed / awk. A considerably expanded set of patterns eventually appeared and was made popular by Perl and other languages.

Some of these implementations attempt to notice when a character could not be magic due to its position.

So, a plain * might search for an actual * and ** then for 0 or more * characters. (And every string has 0 or more...)


Note: Yes, there is a Posix standard but it has so little influence that it can be disregarded.


Every string contains 0 or more repetitions of every other string.


? or * by themselves will do nothing as they have nothing to process. ** and ?* are bad form and should not be used. Anything that compile regex strings properly should error out when presented with either. Strict compilers will error with ? or * alone as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜