Linux07--shell programming 03 wildcard characters and regular expressions

Source: Internet
Author: User

Wildcard characters

Wildcard characters can be used in place of a character. Typically, the asterisk "*" matches 0 or more characters, a question mark "?" Matches a 1 character.

Use case:

1. Files and directories

When describing file names (or paths) in cp/m,DOS,Microsoft Windows , and Unix-like operating systems , the asterisk (*) Match 0 or more arbitrary characters, question marks (?) Matches any character that is 0 or less than the number of question marks. such as: 123??? Will match 1231 or 12313, but will not match 123991991. In Unix shell and Windows PowerShell, the range of characters enclosed in square brackets ("[" and "]") matches any character in this range, for example: [a-za-z] matches any uppercase or lowercase letter. Unix shells can add a "!" before the specified character in parentheses. To express the negation.

2. Database

In SQL, wildcards can be used in "like" expressions, with percent signs (%) matching 0 or more characters, and an underscore (_) to match a single character. The use of square brackets is also supported in Transact-SQL

("[" and "]") to match the list set and the range of characters, preceded by a ^ to indicate negation, will match all characters not specified in parentheses. In Microsoft Access, wildcards can be used in a "like" expression, where an asterisk (*) matches 0 or more characters, a question mark (?). Matches a single character.

Regular expressions

RE character Meaning and examples
^word The string to be searched (word) at the beginning of the line!

Example:grep-n ' ^# ' regular_express.txt
Search for the line beginning with # Start!

^ Indicates a reverse selection in [], no longer indicates positioning at the beginning of the line

word$ String to search (word) at the end of the line!
Example:grep-n '!$ ' regular_express.txt
End of Line! The ranks of the one!
. Represents "any" character, must be an arbitrary character!
Example:grep-n ' e.e ' regular_express.txt
The searched string can be (Eve) (Eae) (EEE) (e e), but not only (EE)! That is, E and E in the middle of "must" only have a character, and empty white space is also a character element!
/ Jump off characters and remove special meanings from special symbols!
Example:grep-n/' Regular_express.txt
Search for the line that contains the single quotes!
* Repeat 0 or more of the previous RE characters
Example:grep-n ' ess* ' regular_express.txt
Find the string containing (es) (Ess) (ESSS) and so on, note that because * can be 0, ES is also compatible with the search string. In addition, because * to duplicate the "previous re character" of the symbol, so, before the * must be bound to a RE character Fu Yi! For example, any character is ". *"!
/{n,m/} The "previous RE character" that continues N to M
If/{n/} is the last RE character of a continuous n,
If/{n,/} is the last RE character that continues to be more than n!
Example:grep-n ' go/{2,3/}g ' regular_express.txt
Between G and G there are 2 to 3 o strings of existence, i.e. (GOOG) (Gooog)
[] The character of the RE special character in the collection of characters
[List]
Example:grep-n ' g[ld] ' regular_express.txt
Search for the line containing (GL) or (GD) ~
It is particularly noted that in [] When the Kan represents a character to be searched,
For example: A[afl]y represents the search string can be Aay, Afy, Aly
i.e. [AFL] stands for a or F or l meaning!

[CH1-CH2]
Example:grep-n ' [0-9] ' Regular_express.txt
Search for the line that contains any number of words! Note that the minus in the character set [] is special meaning, and he represents all the continuity characters between the two characters! But this continues to be related to ASCII encoding, so your code needs to be correct (in bash, you need to make sure that LANG and LANGUAGE are correct!) For example, all large write characters are [a-z]

[^]
Example:grep-n ' oo[^t] ' regular_express.txt
The search string can be (Oog) (Ood) but not (oot), that ^ in [] inside, the meaning of the representation is "reverse choice" means ~ For example, I do not write large characters, that is [^a-z] ~ But, it is important to note that if the grep-n [^a-z] Regul Ar_express.txt to search, and discovered that all the lines in the file were listed, why? Because this [^a-z] is the meaning of "non-large characters", because each line has a non-large character, such as the first line of "Open Source" there is p,e,n,o .... And so on the small write character, as well as the double-quotes (") and other characters, so certainly in line with [^a-z] search!

Regular Expressions (Regular expression) are an expression that is used to find, replace, or delete one or more rows of strings through the arrangement of some special characters.

The regular expression is not a tool or a program, but a string processing basis, many text editing tools under Linux support this standard, such as VI.

Linux07--shell Programming 03 wildcard characters and regular expressions

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.