Regular expressions in the shell

Source: Internet
Author: User

The essence of the shell is to convey a series of commands or signals to the system, usually in the form of a command plus a large string of characters. This string of characters is the character used to express meaning, which is the regular expression. The most basic elements of regular expressions are metacharacters, which are characters used to describe characters, the purpose of which is to describe the contents, explanations, and various operational information.


The Linux shell itself does not support regular expressions and must be implemented using a variety of shell commands and tools, which mainly describe the composition of regular expressions and the meanings of each meta-character.


#正则表达式的用途

Regular expressions are usually used in querying or finding a class of files, filtering information that does not meet the conditions of an expression, and outputting information or data that matches an expression.


#正则表达式的基本元素

The basic element of the regular expression contains the normal character and metacharacters characters

Ordinary characters: ordinary characters, including letters, numbers, etc., where letters are case-sensitive.

Metacharacters: Contains characters that give special effect to * 、.、 ^, [], $, \, \<\>, etc.


#元字符的作用

* Used to match 0 or more repetitions of a previous normal character

such as wo*f can indicate Woof can also represent wooooof, etc.

. Used to match any character, and if there are more than one character, you can use multiple. Symbols consecutively.

such as. He can indicate she can also represent Ahe, BHE, etc., if more than one character can be represented as ... he

Indicates that he has three characters in front of it

^ is used to represent the beginning of a line match, that is, the character at the beginning of the line is "^" and can be one or more

If ^txt indicates a line starting with TXT

[] for matching symbol set, support exhaustive method lists all the elements of the character set, while supporting the "-" to denote the word

The range of the range is "-" left to the right of the character range.

If [0123456] represents any number that matches 0 to 6, it can also be expressed as [0-6]

$ is used to represent the end of line match, and the trailing character is the preceding character, which can be one or more

If txt$ represents a line ending with TXT, it is important to note that the position of the metacharacters is opposite to the beginning of the line.

\ Defines the escape character, which is the special meaning of masking a meta-character, that is, the characters after "\" no longer have special meaning, just ordinary characters.

The \<\> exact match, in the form of a \< to match the content \>. The effect is the result of the output exact match.

such as \<a.txt\> will only output a.txt, and the other results, whether it contains a or TXT, will not be output.

\{\} series of matches, which are available in three forms, as follows.

\{n\} matches the result of n occurrences of the preceding character

If hel\{1\}o means repeat l once, that is, the match result is Hello

\{n,\} matches the result of at least n occurrences of the preceding character

If hel\{3,\}o, the match result is Hellllo

\{n,m\} matches the result of N to M occurrences of the preceding character

such as Hel\{1,3\}o, the output is Hello, Helllo, Hellllo.

In addition to the metacharacters of the regular expressions listed above, there are some extended meta characters that are usually used less often.


#帮助信息:

#Edit by Lion @2015/07/30

#QQ: 2022215673

#Mail: [Email protected]

This article is from the "Essence of the" blog, please be sure to keep this source http://simy88.blog.51cto.com/7370552/1680333

Regular expressions in the shell

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.