Background
I don't know much about regular expressions. Today I have encountered another strange problem. Let's look at the example below.
[root@devserver ~]# echo application |grep k*napplication
I haven't understood it for a long time. I don't know the letter k in the application. It will match. It is found that no matter whether k is replaced with any letter, the match will be successful.
Then I read the usage carefully from Baidu Encyclopedia:
* |
Matches the previous subexpression zero or multiple times. For example, zo * can match "z" and "zoo ". * Is equivalent to {0 ,}. |
After careful understanding that zo * matches z In the example, I finally understand the letter in front of * at any time. Therefore, it is correct in the previous example.
For more information, see the following execution results.
[Root @ devserver ~] # Echo application | grep a * napplication [root @ devserver ~] # Echo application | grep ap * n // It can only match an apn appn appppn [root @ devserver ~] # Echo application | grep ap. * n // here. represents any character, so. * can represent any character in the middle application [root @ devserver ~] # Echo application | grep app. * n // same as the application
Next, replace it with [a-z .*
[root@devserver ~]# echo application | grep a.*napplication[root@devserver ~]# echo application | grep a[a-z]*napplication[root@devserver ~]# echo application | grep a[a-z1-9]*napplication
Use: lower: replace. *.
[root@omsdevserver ~]# echo application | grep app[[:lower:]]*napplication[root@omsdevserver ~]# echo application | grep app[[:upper:]]*n[root@omsdevserver ~]# echo application | grep app[[:upper:][:lower:]]*napplication
Why do we find it strange that regular expressions are rarely used in daily work. However, there are many shells, such as searching for files.
[root@omsdevserver ~]# ls in*loginstall.log install.log.syslog
But we never use the install. log File
[root@omsdevserver ~]# ls ik*log
Therefore, we believe that the grep ik * log cannot be matched and found by searching for data. * In shell refers to expansion. We use the most commonly used Filename expansion.
After word splitting, unless the -f option has been set (see Section 2.3.2), Bash scans each word for the characters "*", "?", and "[". If one of these characters appears, then the word is regarded as a PATTERN, and replaced with an alphabetically sorted list of file names matching the pattern
In this case, it is not a regular expression rule, but a PATTERN.