Shell Scripting Learning-Regular expressions

Source: Internet
Author: User
Tags character classes egrep

    • The concept and characteristics of regular expressions
A regular expression is a logical formula for a string operation, which is to make a "rule string" with a predefined set of characters and a combination of those specific characters.This "rule string" is used to express a filter for a string. given a regular expression and another string, we can achieve the following purposes:1. Whether the given string conforms to the filtering logic of the regular expression (called "Match");2. You can get the specific part we want from the string using a regular expression. regular expressions are characterized by:1. Flexibility, logic and functionality are very strong;2. Complex control of strings can be achieved quickly and in a very simple way. 3. For people who have just come into contact, it is more obscure and difficult to understand. because regular expressions are primarily applied to text, they are applied in a variety of text editor scenarios, from the famous editor editplus to large editors such as Microsoft Word, Visual Studio ,you can use regular expressions to manipulate text content.
    • Regular Expression components
1. Character class (Character Class): As in the example above X and Y, they are in the pattern table? A character, but the range of values is a class of wordsany one of them.
2. Number Qualifier (quantifier): Each part of the mail address can have one or more x characters, each part of the IP addresscan have 1-3 y characters
3. Position qualifier (Anchor): Describes the position relationship between various character classes and ordinary characters, such as the three-letter addresssection, separated by ordinary characters @ and., IP addresses are divided into four parts,to separate each part with a character class and a number qualifier.description. In order to represent positional relationships, the concept of positional qualifiers is required, and the following polygons are introducedspecifies that some special syntax represents a character class, a quantifier, and a positional relationship, and then uses these special grammars to represent a pattern together with ordinary characters, which is the regular expression (Regular expressions). For example, the regular expression of an email address can be written as [A-za-z0-9_.-][email protected][a-za-z0-9_.-]+\. [The regular expression of an A-za-z0-9_.-]+,ip address can be written as[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}. Then we introduce the syntax of regular expressions, so let's look at how regular expressions are used in grep. For example have such a text? piece Testfile:find the line that contains the IP address:What is grep? A lookup filter? tool, regular expressions in grep? To find a string that conforms to the pattern.
egrep equivalent to grep-e, table? extended the regular expression syntax. Grep's regular expression has basic and extended two kinds of specifications. There is also the Fgrep command, the equivalent of grep-f, the table shows only fixed string search? The regular expression pattern is not searched, and the following parameters are not interpreted according to the syntax of the regular expression.
192.168.1.1
1234.234.04.5678
123.4234.045.678
ABCDE$ Egrep ' [0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3} ' testfile
192.168.1.1
1234.234.04.5678
Note that the regular expression parameter is enclosed in single quotes, because many special characters in a regular expression have special meanings in the shell (for example, \) .only the single quotation marks can guarantee that these characters are passed intact to the grep command without being interpreted by the shell. 192.168.1.1 conforms to the above pattern, consists of three. Separated four paragraphs, each of which is 1 to 3 digits, so this one is found out, but why is 1234.234.04.5678 also found out?because grep is looking for a pattern? contains a string 234.234.04.567 that conforms to the pattern. Instead, 123.4234.045.678 does not contain a string that conforms to the pattern, so it will not be found. grep is a lookup filtering tool in which regular expressions find strings that conform to patterns in grep. In fact, the regular expression also has an important should be? is to verify that user input is legitimate,For example, users submit their e-mail address through a Web form, they need to use a program to verify that it is not a legitimate email address, the work can be done in the Web page JavaScript, or in the website backstage program, such as PHP, Perl, Python, Ruby, Java, or C, all these words? Support for regular expressions, it can be said that the current programming language does not support regular expressions is very rare. In addition to the programming language , many UNIX commands and tools also support regular expressions such as grep, VI, SED, awk, Emacs, and so on. a regular expression, like a variable, is a generic concept, not an attribute of a tool or programming language.
    • Regular expression basic syntax (as an example of grep's regular expression)
[character class] Example:[Quantity qualifier]

Example:

again, grep is looking for a row that contains a pattern, rather than a row that exactly matches a pattern.

   [position qualifier] Example:[Special characters]Example:

Shell Scripting Learning-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.