A detailed description of the regular expression of Linux Basic learning -10.3-

Source: Internet
Author: User

First, the regular expression RE regular expression1, what is the regular why use it?

How can you choose the ID number in this?

440304199604012792

130528197108126121

3605sss98304033896

342923198310042132

1404ddddddddd5694x

61242619860416291X

5002xxxxxx04279521

330900199806382320

654126197703092303

131127197105115662

Numbers with X (in the last one)

Find a variety of words with symbol matching.

The regular expression is passed the special symbol ^ $ []. * denotes a variety of words.

It is convenient for us to process the text (log).

2, the regular use of the range

Who can use the regular?

Three Musketeers Regular (grep sed awk)

Python Java

3. The difference between regular expressions and wildcard characters

Regular-----filtering in Files (find file contents) Three Musketeers support

Wildcard---Find files (filenames) Most commands can be used

4, the use of regular precautions

1) The regular default is handled according to the behavior unit.

2) Be careful not to use Chinese symbols.

. "" "^" () {} [] <>

。 ‘’“”......?? ( ){}【】《》

3) Add aliases to Grep/egrep

Cat >>/etc/profile<<eof

Alias grep= ' grep--color=auto '

Alias egrep= ' Egrep--color=auto '

Eof

Source/etc/profile

5. Regular classification

Base Regular: ^ $. * []

Extended Regular: + | () {} ?

Basic regular grep sed

Extended regular Egrep Sed-r awk

For the Three Musketeers of Linux

Handled in a behavioral unit

Cat >>/etc/profile<<eof

Alias egrep= ' Egrep--color=auto '

Alias grep= ' grep--color=auto '

Eof

Source/etc/profile

Export Lc_all=c

Test file

Cat >>~/test/oldboy.log<<eof

I am Oldboy teacher!

I Teach Linux.

I like badminton ball, billiard ball and Chinese chess!

My blog ishttp://oldboy.blog.51cto.com

Our site ishttp://www.etiantian.org

My QQ num is 49000448.

Not 4900000448.

My God, I am not oldbey,but oldboy!

Gd

Good

God

Goood

Gooood

Oldboy1

Eof

Second, the Foundation Regular Foundation Regular (BRE) First wave character description

1. ^word Match start Word

2. word$ matches Word end

3, ^$ match beginning end is empty line

[[email protected] test]# grep "^m" Oldboy.log

My blog ishttp://oldboy.blog.51cto.com

My QQ num is 49000448.

My God, I am not oldbey,but oldboy!

[[email protected] test]# grep "m$" Oldboy.log

My blog ishttp://oldboy.blog.51cto.com

[Email protected] test]# grep-n "^$" Oldboy.log

3:

8:

[[email protected] test]# grep "^m" Oldboy.log

My blog ishttp://oldboy.blog.51cto.com

My QQ num is 49000448.

My God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n "^$" Oldboy.log

3:

8:

[[email protected] test]# grep "Oldboy" Oldboy.log

I am Oldboy teacher!

My blog ishttp://oldboy.blog.51cto.com

[[email protected] test]# grep "Oldb.y" Oldboy.log

I am Oldboy teacher!

My blog ishttp://oldboy.blog.51cto.com

My God, I am not oldbey,but oldboy!

[[email protected] test]# grep "Oldb.y" Oldboy.log-o

Oldboy

Oldboy

Oldbey

[[email protected] test]# grep ". $" Oldboy.log

I Teach Linux.

My QQ num is 49000448.

Not 4900000448.

Basic Regular (BRE) Second wave character description

. Represents and can only represent any one character

Translating symbols, special characters to restore the original meaning

\ n = line break

\ n used in echo sed awk

* Match one of the preceding characters 0 or more times (any number of times)

. * Match any one character any number of times (any string)

^.* matches the beginning of any string

[[email protected] test]# grep "^.*o" Oldboy.log

I am Oldboy teacher!

I like badminton ball, billiard ball and Chinese chess!

My blog ishttp://oldboy.blog.51cto.com

Our site ishttp://www.etiantian.org

Not 4900000448.

My God, I am not oldbey,but oldboy!

Description: If the matching content is in more than one row, grep will match from left to right to the last, and the more

Tip: A summary of the special meanings of points (.):

1. Current directory

2, make the file effective equivalent to source

3, the beginning of hidden files

4, any one character

[Email protected] test]# grep-n "." Oldboy.log

2:i Teach Linux.

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

[Email protected] test]# grep-n "." Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n ". *" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

3:

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

8:

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

Basic Regular (BRE) Third Wave character description

[ABC] matches A, B, c any one character

[^ABC] matches non-A, non-B, non-C any one character

A{n,m} matches a this character N to M times

A{n,} matches a character at least n times

A{n} matches a this character n times

A{,m} matches a this character up to M times

Note: The above is escaped, but the Egrep (GREP-E) or Sed-r or awk {} does not need to be escaped

[Email protected] test]# grep-n "[ABC]" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

10:my God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n "[^abc]" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n "[a-z0-9]" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n "^[ABC]" Oldboy.log

[Email protected] test]# grep-n "[^abc]" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

[Email protected] test]# grep-n "^[^ABC]" Oldboy.log

1:i am Oldboy teacher!

2:i Teach Linux.

4:i like badminton ball, billiard ball and Chinese chess!

5:my Blog ishttp://oldboy.blog.51cto.com

6:our site ishttp://www.etiantian.org

7:my QQ num is 49000448.

9:not 4900000448.

10:my God, I am not oldbey,but oldboy!

[[email protected] test]# grep "[^a-z]" Oldboy.log

I am Oldboy teacher!

I Teach Linux.

I like badminton ball, billiard ball and Chinese chess!

My blog ishttp://oldboy.blog.51cto.com

Our site ishttp://www.etiantian.org

My QQ num is 49000448.

Not 4900000448.

My God, I am not oldbey,but oldboy!

[[email protected] test]# grep "[^0-9]" Oldboy.log

I am Oldboy teacher!

I Teach Linux.

I like badminton ball, billiard ball and Chinese chess!

My blog ishttp://oldboy.blog.51cto.com

Our site ishttp://www.etiantian.org

My QQ num is 49000448.

Not 4900000448.

My God, I am not oldbey,but oldboy!

[[email protected] test]# grep "[^0-9]" Oldboy.log

I am Oldboy teacher!

I Teach Linux.

I like badminton ball, billiard ball and Chinese chess!

My blog ishttp://oldboy.blog.51cto.com

Our site ishttp://www.etiantian.org

My QQ num is 49000448.

Not 4900000448.

My God, I am not oldbey,but oldboy!

[Email protected] test]# grep-v "[0-9]" Oldboy.log

I am Oldboy teacher!

I Teach Linux.

I like badminton ball, billiard ball and Chinese chess!

Our site ishttp://www.etiantian.org

My God, I am not oldbey,but oldboy!

[[email protected] test]# grep "[0-9]" Oldboy.log

My blog ishttp://oldboy.blog.51cto.com

My QQ num is 49000448.

Not 4900000448.

[[email protected] test]# grep "0{2,3}" Oldboy.log

My QQ num is 49000448.

Not 4900000448.

[[email protected] test]# grep-o "0{2,3}" Oldboy.log

000

000

00

[[email protected] test]# grep "0{1,5}" Oldboy.log

My QQ num is 49000448.

Not 4900000448.

[[email protected] test]# grep-o "0{1,5}" Oldboy.log

000

00000

[[email protected] test]# egrep "0{1,5}" Oldboy.log

My QQ num is 49000448.

Not 4900000448.

[[email protected] test]# egrep-o "0{1,5}" Oldboy.log

000

00000

Third, the expansion of regular

. Match the previous character one or more times

A + matches a this character one or more times

? Match a previous character 0 or 1 times

A? Match a This character 0 or 1 times

| Represents or is used to simultaneously filter multiple

() group back to reference

Egrep "0+" Oldboy.txt

Egrep-o "0+" Oldboy.txt

Egrep-o "[a-z]+" Oldboy.txt

Egrep "[a-z]+" Oldboy.txt

Egrep-o "[a-z]+" Oldboy.txt

Egrep-o "[a-za-z]+" Oldboy.txt

Egrep-o "[a-z]+" Oldboy.txt

[Email protected] test]# egrep "Go?d" Oldboy.log

My God, I am not oldbey,but oldboy!

Gd

God

[Email protected] test]# egrep "Go+d" Oldboy.log

My God, I am not oldbey,but oldboy!

Good

God

Goood

Gooood

[Email protected] test]# egrep "Go*d" Oldboy.log

My God, I am not oldbey,but oldboy!

Gd

Good

God

Goood

Gooood

[Email protected] test]# egrep "Go{0,3}d" Oldboy.log

My God, I am not oldbey,but oldboy!

Gd

Good

God

Goood

[Email protected] test]# dumpe2fs/dev/sda1 |grep-i "inode size"

DUMPE2FS 1.41.12 (17-may-2010)

Inode size:128

[[email protected] test]# dumpe2fs/dev/sda1 |grep-i "Block Size"

DUMPE2FS 1.41.12 (17-may-2010)

Block size:1024

[[email protected] test]# dumpe2fs/dev/sda1 |grep-i "inode count"

DUMPE2FS 1.41.12 (17-may-2010)

Inode count:51200

[[email protected] test]# dumpe2fs/dev/sda1 |grep-i "Block Count"

DUMPE2FS 1.41.12 (17-may-2010)

Block count:204800

Reserved Block count:10240

[Email protected] ~]# echo "##**##@@##@##*#[email protected]@@@@@@2**@@@**##**" |egrep "[#@*]+]

##**##@@##@##*#[email protected]@@@@@@2**@@@**##**

[[email protected] ~]# echo "##**##@@##@##*#[email protected]@@@@@@2**@@@**##**" |egrep "[#@*]+"-O

##**##@@##@##*#

@@@@@@@

**@@@**##**

[Email protected] test]# egrep "oldb (o|e) y" Oldboy.log

I am Oldboy teacher!

My blog ishttp://oldboy.blog.51cto.com

My God, I am not oldbey,but oldboy!

Oldboy1

[Email protected] test]# dumpe2fs/dev/sda1 |egrep-i "(inode|block) Size"

DUMPE2FS 1.41.12 (17-may-2010)

Block size:1024

Inode size:128

[[email protected] test]# dumpe2fs/dev/sda1 |egrep-i "(inode|block) Count"

DUMPE2FS 1.41.12 (17-may-2010)

Inode count:51200

Block count:204800

Reserved Block count:10240

[Email protected] test]# dumpe2fs/dev/sda1 |egrep-i "(Inode|block) (Size|count)"

DUMPE2FS 1.41.12 (17-may-2010)

Inode count:51200

Block count:204800

Reserved Block count:10240

Block size:1024

Inode size:128

Exercises:

1, what is the role of regular and regular?

2, the relevant symbols of the basic regular

3, extension of the relevant symbols of the regular

A detailed description of the regular expression of Linux Basic learning -10.3-

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.