Easy to ignore Regular Expression usage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.