Strawberry
Strawberry

Reputation: 67928

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 nothing 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

Upvotes: 0

Views: 173

Answers (3)

DigitalRoss
DigitalRoss

Reputation: 146151

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.

Upvotes: 2

user481081
user481081

Reputation:

? 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.

Upvotes: 0

tchrist
tchrist

Reputation: 80433

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

Upvotes: 1

Related Questions