Shell wildcard wildcards Regular expression understanding and difference

Source: Internet
Author: User

First, the regular expression:

Metacharacters is a character used to illustrate the meaning of a character expression, in short, a character used to describe a character.

The regular expression re (Regular expression) is a string of characters consisting of a string of character and metacharacters.

The primary function of regular expressions is text query and string manipulation, which can match a character or character set of text. In fact, the regular expression completes the filtering of the data, rejecting the data that is not satisfied with the regular expression definition, leaving the data that matches the regular expression.

Basic regular Expression meta-character set and its significance

Symbol Significance
* 0 or more ordinary characters before the * character and the preceding character can be considered as one character
. Match any character
^ Matches the beginning of the line, or the character following the non-
$ Match end of Line
[] Match Character Set fit
\ Escape character, shielding the special meaning of a meta-character
\<\> Exact match symbol
\{n\} matches the preceding character n times p.43
\{n,\} Matches the preceding character at least n times
\{n,m\} Match the preceding character appears n~m times

1. The "*" symbol (and the preceding character can be seen as a character)

Zha*ng: "*" in front of the ordinary character is a, which means match a character 0 or more times, such as: Zhng, Zhang, Zhaaaang

( according to the test can not include the preceding characters, that is, matching 0 times )

2. Match Blank Lines

The "^$" that matches the ^ at the beginning of the line and the end of the matching lines can be used to match a blank line, and if you need to match a line that contains only one character, you can use "^.$"

3. "[]" symbol

Matches the character set, supports exhaustive methods to list all the elements of a character set, and also supports the use of the "-" notation to denote the range of character sets .

[12345] equivalent to [1-5]

We know that the "^" symbol matches the beginning of the line, but the "^" symbol is placed in the "[]" symbol and no longer matches the beginning of the match, but instead represents the inverse symbol.

[^b-d]: Indicates that characters in the range are no longer b~d

[A-za-z] [a-za-z]*: Matches any English word

Second, the extension of the regular expression:

Extended regular expression meta-characters and their meanings

Symbol Significance
Match 0 or one of the ordinary characters before it
+ Match one or more of the ordinary characters preceding it
() Represents a character set or used in expr
| Represents "or", matching a set of optional characters

1. "? "Symbol

( according to the test can not include the preceding characters, that is, matching 0 times )

2. "()" Symbol and "|" Symbol

"()" Symbol and "|" Symbols are typically used in conjunction to represent a set of optional characters.

Re (a|b|c) d is equivalent to Re[abc]d

"|" A symbol can also represent a "or" relationship of more than one regular expression.

Grep-e "Zha? Ng|zhi "File

Three, wildcard characters:

The bash shell itself does not support regular expressions, and the use of regular expressions is shell commands and tools, such as Grep,sed,awk.

Note: The bash shell can only implement the wildcard (Globbing) function with some metacharacters in the regular expression.

A wildcard is the process of extending a batch of specific filenames to a non-specific file name that contains wildcard characters.

Summary: it can be seen that the main application of the wildcard matches the file name, while the regular is mainly applied to the string

*-wildcards, which represent any character (0 to more)
? -wildcards, which represent one character

^ Take the reverse

\-Jump symbol to restore special characters or wildcards to a general symbol
| -demarcation of two pipeline commands
; -Definition of continuity order
~-User's root directory
$-Variable value to be added before the variable
! -"Non" (not) in logical operation
'-single quote, no variable substitution function
"-double quotes with variable displacement function


This article is from the blog of "Kun-brother", please make sure to keep this source http://kunge.blog.51cto.com/976628/1585958

Shell wildcard wildcards Regular expression understanding and difference

Related Article

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.