Regular Expression learning notes series ---- 1

Source: Internet
Author: User


A regular expression is a character pattern. When matching the content of a text file, it uses a special set of symbols to match the content we want.


Why do I use regular expressions?

Search (MATCH) the content we want to see.


Metacharacters of the regular expression:


Metacharacter function example

^ The first line locator/^ root/matches the line starting with Root

$ Line tail locator/bash $/match the line ending with Bash

. Match a single character/L. ve/match love, live, etc. There is a character between l and V

* Leading character/LO * SE/matching LSE, lose, loose, etc.

Match 0 or more characters before it (string)

/L */matches 0 l, or l, ll, and so on.

/Ll */match l, ll, lll, etc.

[] Match any one of a group of characters/L [OI] VE/match love, live

[X-y] indicates a range of/[A-C] OVE/matches aove, Bove, Cove

Where X and Y are letters or numbers/[0-9] 2/match,..., 92

[^] Indicates reverse/[^ TT] om/matches

\ Indicates escape/2 \. 0/matches 2.0. At this time,. is a common character, no longer a Metacharacter

/Love./match love.

\ <Start separator/\ <love/matches loved and lovely

\> The ending separator/love \>/matches inlove and so on

X \ {M \} X characters repeated m times/A \ {5 \}/matching aaaaa

The characters x \ {M, \} X must be repeated at least m times/A \ {5, \}/matches AAAAA, aaaaaa, aaaaaaa, and so on

X \ {M, n \} the preceding X contains M-N // \ <[A-Za-Z] \ {5, 7 \} \> or/A \ {5, 7 \}/matches AAAAA, aaaaaa, aaaaaaa

(3-5) indicates three or four or five matches (the range of M and N is 0-255 .)

. * Any 0 or multiple characters

\ (... \) Label lovely \ (love \) ly \ 1ly




How to Learn:

Let's look at the example first, convert it, modify it, and change it to yourself.


Syntax format:

Command "Regular Expression" Text File

Grep "^ root" passwd



Examples of Regular Expressions:

1 ).

[[Email protected] TMP] # Cat test2.txt

Lovely

Livl

Levllaeq

Lcveroop

Woeijfdklkiod

Love Mo forerver

Hahahh lilylilei

Ku Hou Haha Xixi

Doqiadddoqiia

[[Email protected] TMP] # grep "C. E" test2.txt

Lcveroop

[[Email protected] TMP] # grep "L. L" test2.txt

Hahahh lilylilei

[[Email protected] TMP] # grep "E. J" test2.txt

Woeijfdklkiod


######################################## ######################################## ######################################## ########

2 )*

[[Email protected] TMP] # Cat test2.txt

Lovely

Livl

Levllaeq

Lcveroop

Woeijfdklkiod

Love Mo forerver

Hahahh lilylilei

Ku Hou Haha Xixi

Zxysypr

Love Mo forervez

[[Email protected] TMP] # grep "lo *" test2.txt

Lovely

Livl

Levllaeq

Lcveroop

Woeijfdklkiod

Love Mo forerver

Hahahh lilylilei

Love Mo forervez

[[Email protected] TMP] # grep "lo * I" test2.txt

Livl

Hahahh lilylilei

[[Email protected] TMP] # grep "lo * o" test2.txt

Lovely

Love Mo forerver

Love Mo forervez


This article from the "empty valley orchid" blog, please be sure to keep this source http://2489843.blog.51cto.com/2479843/1538914

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.