Technical examples of regular expressions

Source: Internet
Author: User
Tags egrep

Linux op Korimasa expression combat

1. What is a regular expression?
To put it simply, regular expressions are a set of rules and methods that are defined to handle a large number of strings.
For example, suppose @ stands for 12345
With these special symbols of regular expressions, we can quickly filter and replace the required content.
Linux is generally handled in a behavioral unit.

2. Why study regular expressions?
re working, we do Linux operations, always facing a lot of log, program, command output.

3. Understand the two error points to note:
A. The application of regular expressions is very extensive and exists in various languages, such as Perl,php,java.
What we are explaining to you today is the regular expression of Linux, the regular expressions commonly used in system operation and maintenance.
The commands for common regular expressions are grep (egrep), Sed,awk.
The Three Musketeers of Linux are very bull, if you want to play better with the Three Musketeers, you must leave the positive expression.

B.linux regular expressions are fundamentally different from the wildcard characters that are used by other commands in our command line.


4.3 Considerations for the experiment:
A.linux are usually dealt with by behavioral units.
B.alias grep= ' grep--color=auto '
C. Note the character set, Lc_all=c, is not required

5.linux Regular Expression details
Based on grep.
Base Regular expression: BRE (basic regular expression)
1) ^word means searching for content that begins with Word.
2) word$ means searching for content that ends in Word.
3) ^$ represents a blank line, not a space.
4). Represents and can only represent any one character. (current directory, loading parts)
5) \ Escape character, let the character with special meaning, take off the vest, restore the prototype.
For example: \. Represents only the decimal point and restores the meaning of the original decimal point.
6) * Repeat 0 or more of the preceding one character. It doesn't mean everything.
7). * Matches all characters. ^.* start with any number of characters.
8) [ABC] matches any one of the characters in the character set [A-z]
9) [^ABC] ^ again inside the brackets indicate non, does not contain a or B or C.
{N,m} repeats n to M times, the previous character.
{N,} at least n times, more than unlimited.
{n} n times
{, m} up to M times, less open.
Note: grep does not need to be escaped to {escape} \{\},egrep (GREP-E)
The basis of the regular is finished.
Summary of 6.grep command parameters
-V Excludes matching content
-e support for extended regular expressions
-I ignores case
-o outputs only matching content
--color=auto matches the content display color.
-N Displays line numbers again at the beginning

7. Take the IP address to practice the regular:

SED basic syntax:
S replacement
G Global
-I modify File
-N cancels the default output
P Print Content
-R supports extended regular

Questions:
Ifconfig eth0:
inet addr:10.0.0.7 bcast:10.0.0.255 mask:255.255.255.0
Target: 10.0.0.7
Filter ideas:
The content before the target "inet addr:" is used ^.*ADDR:
The target end of the content "bcast:10.0.0.255 mask:255.255.255.0" is used bc.*$
Command combat:
Special direct sed fetch line *:
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2s#^.*addr:# #gp ' |sed ' s# bc.*$# #g '
10.0.0.7
[Email protected] oldboy]#ifconfig eth0|sed-n ' 2s#^.*addr:\ (. *\) BC.*$#\1#GP '
10.0.0.7
[Email protected] ~]# ifconfig eth0|sed-rn ' 2s#^.*addr: (. *) BC.*$#\1#GP '
10.0.0.8
[Email protected] oldboy]# ifconfig eth0|sed-n ' 2s#^.*addr:\ ([0-9]\{,3\}\.[ 0-9]\{,3\}\. [0-9]\{,3\}\. [0-9]\{,3\}\] BC.*$#\1#GP '
10.0.0.7

Questions:
Please use ifconfig, output IP and broadcast address
Sed-n ' s#\ (dddd\) \ (ffff\) #\1\2#gp ' is also suitable for grep.
Command:
[[email protected] ~]# ifconfig eth0|sed-nr ' S#^.*DR: (. *) [A-za-z].*t: (. *) MA.*$#\1\2#GP '
10.0.0.8 10.0.0.255

Extended Regular Expressions: Grep-e and Egrep
Have a chance to share it later!
Here is the post-supplement, the lecture has no time to talk about.
8. Extended Regular expression: ERE
1) + Repeat one or more of the preceding characters.
2)? Repeat 0 or one of the 0 preceding characters.
3) | Find multiple matching strings in a way or
4) () find the "user group" string.
Another knowledge to know: POSIX square bracket Character set (pretty chicken knows it)
[: Alnum:]          [: Lower:] [: Xdigit:]
[: Alpha:]            [:p rint:] [: Blank:]
[: Blank:] [:p UNCT:]
[: Cntrl:] [: Space:]
[: Graph:] [:: Upper:]
5) Man Bash
Mans grep


Technical examples of regular expressions

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.