简体   繁体   中英

Confused about this regular expression

Okay, so ^[AZ] means beginning with a capital letter. So what does ^[AZ]* mean? Does it not mean zero or more occurrences of a capital letter? Because it is really confusing me since it is including the empty line in the output which is not a capital letter. Also, could you explain ^[AZ]*$ ?

mugbear:~# clear
mugbear:~# cat emptyspace 
line1
line2

line4
line5

line7
mugbear:~# grep '^[A-Z]*' emptyspace                                            
line1
line2

line4
line5

line7
mugbear:~# grep '^[A-Z]*$' emptyspace


mugbear:~# 

An empty line is zero or more occurrences of a capital letter. The latter expression is ambiguous, starts and ends with zero or more occurrences of a capital letter, also known as "anything."

Update : Please refer to Tanzelax's answer as the answer that should have been accepted.

Zero or more occurrences can include zero occurrences.

Thus, ^[AZ]* includes just 'new line', which is every line.

$ is end of line, so ^[AZ]*$ means 'new line, followed by any number (including zero) of capital letters, followed by an end of line', which is only the blank lines (which are 'new line, zero capital letters, end of line').

If you're asking what the addition of the asterisk to the end of the expression does, it means to match 0 or more times. In the expression that you provided it means to match as many consecutive capital letters as possible.

http://www.regular-expressions.info/ might also be of some help to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM