Linux Regular expressions

Source: Internet
Author: User

1. What is a regular expression?
In short, regular expressions are a set of rules and methods that are defined to handle a large number of strings.
For example: Suppose "@" stands for Oldboy, "! "Representative Oldgirl,echo" @! "= = =" Oldboyoldgirl "
By defining the auxiliary of these special symbols, the system administrator can quickly filter, replace or output the required strings, and Linux regular expressions are generally handled in the behavioral unit.

2, why should learn the regular expression?
In the enterprise work, we do every day in the Linux operations, the moment will face a large number of text configuration with strings, programs, command output and log files, etc., and we often have an urgent need, from a large number of string content to find a specific string matching the job needs. This depends on the regular expression. Therefore, it can be said that the regular expression is to filter the needs of such a string born!
For example: Ifconfig output IP, for example: cat/var/log/messages output, and so on.

3, easy to confuse two precautions
Regular expressions are widely used in a variety of languages, such as Php,python,java. However, today we are talking about regular expressions in Linux system operations, that is, Linux regular expressions.
The most commonly used regular expression command is grep (egrep), SED, awk, in other words, the Linux Three Musketeers to work more efficient, it must be inseparable from the regular expression of cooperation.
Regular expressions and our usual wildcard characters are essentially different.
Wildcard example: LS *.log here is the wildcard character (for all), not a regular expression
Precautions:
1) Linux is normally handled in a behavioral unit
2) define aliases alias grep= ' grep--color=auto ', take grep as an example
3) Note the character set, Lc_all=c

4, the basic regular expression
Basic Regular expression: meta-character meaning BRE (basic regular expression)
^word means searching for content that begins with Word
word$ means searching for content ending in Word
^$ represents a blank line, not a space
. expressed in the regular and can only represent any one character ↓

Other meanings: 1, current directory; 2, make the file effective, equivalent to source;3, hidden files
\ escape character, let the character with special meaning, take off the vest, restore the prototype
* Repeat 0 or more of the preceding characters
. * matches all characters, including blank lines. ^.* start with any number of characters
[ABC] matches any character in a character set
[^ABC] ^ represents non within parentheses, does not contain any character set in parentheses, and
{} {N,m} repeats the previous character N to M times
{N,} repeats the previous character at least n times
{n} repeats the previous character n times
{, m} repeats the previous character at most m times (CentOS6.7 can begin)
Note: grep requires no escaping for {} escape \{\},egrep (GREP-E)

5, actual combat examples

[[email protected] oldboy]# cat oldboy.log i am oldboy1 teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq  Num is 49000448.not 4900000448.my god ,i am not oldbey,but oldboy ! [[email protected] oldboy]# grep  "^m"  oldboy.logmy blog is http:// oldboy.blog.51cto.commy qq num is 49000448.my god ,i am not  oldbey,but oldboy! [[email protected] oldboy]# grep  "m$"  oldboy.log my blog is http ://oldboy.blog.51cto.com[[email protected] oldboy]# grep  "^$"  oldboy.log [[email  protected] oldboy]# grep  "."  oldboy.logi am oldboy1  teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq  Num is 49000448.not 4900000448.my god ,i am not oldbey,but oldboy ! [[email protected] oldboy]# grep  "\."  oldboy.logi teach linux.my blog is http://oldboy.blog.51cto.comour site  is http://www.etiantian.orgmy qq num is 49000448.not 4900000448. [[email protected] oldboy]# grep  ". *"  oldboy.log I am oldboy1  teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq  num is 49000448.not 4900000448.my god ,i am not oldbey,but oldboy! [[email protected] oldboy]# grep  "[0-9]"  oldboy.log   I am  oldboy1 teacher!my blog is http://oldboy.blog.51cto.commy qq num is  49000448.not 4900000448. [[email protected] oldboy]# grep  "[^0-9]"  oldboy.log i am oldboy1  teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq  Num is 49000448.not 4900000448.my god ,i am not oldbey,but oldboy ! [[email protected] oldboy]# grep -e  "0{1,2}"  oldboy.log my qq num  is 49000448.not 4900000448.

6. Extended Regular Expressions

Extended Regular expression: ERE (Extended Regular Expressions)
+ Repeat one or more of the preceding characters
? Repeat 0 or one of the preceding characters
| Find multiple matching strings in a way or
() group filter, back reference
7, actual combat examples

[[email protected] oldboy]# grep -e  "o+"  oldboy.logI am oldboy1  teacher! I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgnot  4900000448.my god ,i am not oldbey,but oldboy! [[email protected] oldboy]# grep -e  "0+"  oldboy.log my qq num  is 49000448.not 4900000448. [[email protected] oldboy]# grep -e  "0?"  oldboy.log i am oldboy1 teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess!my blog  is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq  Num is 49000448.not 4900000448.my god ,i am not oldbey,but oldboy! [[email protected] oldboy]# grep -e  "Oldboy|oldbey"  oldboy.logI am  oldboy1 teacher!my blog is http://oldboy.blog.51cto.commy god ,i am  not oldbey,but oldboy! [[email protected] oldboy]# grep -e  "Old (Boy|bey)"  oldboy.logI am  oldboy1 teacher!my blog is http://oldboy.blog.51cto.commy god ,i am  not oldbey,but oldboy! metacharacters: \b Word boundaries [[email protected] oldboy]# grep  "OLDBOY"   oldboy.log i am oldboy1 teacher!my blog is http://oldboy.blog.51cto.com[[ email protected] oldboy]# grep  "\boldboy\b"  oldboy.log my blog is  http://oldboy.blog.51cto.com


This article is from the "architects of the Day" blog, be sure to keep this source http://wanyuetian.blog.51cto.com/3984643/1716936

Linux 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.