Linux Regular expressions

Source: Internet
Author: User
Tags alphabetic character posix

Before you know the Linux regular expressions, you need to know the difference between the wildcard wildcards regular in Linux: Wildcards are used primarily for bash environments, while the regular majority is used in the Linux Musketeers (grep, sed, awk) ... and Linux in the regular and other development environment of the regular is different, we temporarily study is only the Linux environment of the regular ...

Wildcard Basics

Wildcard characters Describe Example
* Any number of characters Find. *.sh
Represents any one character Find. ?. Sh
Continuous different command separation ls m1;ls m2
# Configuration file comments # # # #crontab # #
| Pipe character Find. -type f-name "*.txt" | Xargs rm-f
~ User's home directory oldboy~
- Last Action Directory Su-
$ Variable symbol $PATH
/ Root /
> redirect echo "Linux" >oldboy.sh
>> Append redirect echo "Linux" >>oldboy.sh
< Input Xargs–n 2 <oldboy.txt
<< Input redirect Xargs–n 2 <<oldboy.txt
What you see in single quotes is what you gain echo ' Date ' ==>date
Double quote variable substitution

echo "Date" ==>date

Num=3
echo "$num" ==>3

` Equivalent to $ ()

LS ==>oldboy.sh
echo ' ls ' ==>oldboy.sh

{] Content sequence Touch m{1..10}
CP File{,.bak}
! Take counter Find. -type f! -name "*.sh"
&& And &&
|| Or ||

Basic composition of regular expressions

Regular expressions Describe Example
^ Match beginning ^ABC matches lines starting with ABC
$ Match end def$ matches a line ending with Def
. Any one character A.C matches ABC, ADC, etc.
\ Escape character, restore character itself meaning \. Represents the point itself
* Repeat 0 or more of the previous characters o* represents 0 o or more O
.* Any character The ^.* represents the beginning of any character
[ABC] Match any one of the characters in the character set [A-z] [A-z] [0-9] any a-z,a-z,0-9
[^ABC] Match all characters that do not contain ^ [^ABC] does not contain all the characters of ABC
A\{n,m\}
A\{n,\}
A\{n\}
Match repeats N to M times a character
Repeat at least n times
Repeat only n times
0\{0,3\} 0 repeated 0-3 times
0\{3,\} Repeat at least 3 times 0
0\{3\} only repeats 3 times 0
() Create a substring for matching MA (tri)? Match Max or Maxtrix

Extended Regular expression (grep–e or egrep)

Regular expressions Describe Example
+ Match 1 or more
(* is 0 or more)
A + matches 1 to multiple a
Match 0 or one
(. is only one)
A? Match 0 A or 1 a
| Filter multiple characters at the same time Grep–e "3306|1521"/etc/services

Metacharacters

Metacharacters (meta character) is a Perl-style regular expression that is supported by only a subset of text processing tools, not all text processing tools.

Regular expressions Describe Example
\b Word boundaries \bcool\b matching cool, mismatched coolant
\b Non-word boundary cool\b matching coolant, not matching cool
\d Single numeric character B\DB match Business-to-business, mismatch BCB
\d Single non-numeric characters B\db matching BCB, mismatch business-to-business
\w Single word characters (letters, numbers, and _) \w match 1 or a, mismatch &
\w Single non-word character \w match &, mismatch 1 or a
\ n Line break \ n matches a new row
\s Single whitespace character X\SX matches x x, does not match xx
\s Single non-whitespace character X\S\X matches Xkx, does not match xx
\ r Enter \ r Match Carriage return
\ t Horizontal tab \ t matches a horizontal tab
\v Vertical tab \v matches a vertical tab
\f Page break \f Match a page break

POSIX character class

The POSIX character class is a special meta-sequence (meta sequence) of a shape such as [: ...:] that can be used to match a specific range of characters

Regular expressions Describe Example
[: Alnum:] Match any one letter or number character [[: alnum:]]+
[: Alpha:] Match any alphabetic character (including uppercase and lowercase letters) [[: Alpha:]] {4}
[: Blank:] Spaces and tabs (Landscape and portrait) [[: blank:]]*
[:d Igit:] Match any numeric character [[:d Igit:]]?
[: Lower:] Match lowercase letters [[: Lower:]] {5,}
[: Upper:] Match uppercase letters ([[: upper:]]+)?
[:p UNCT:] Match punctuation [[:p UNCT:]]
[: Space:] Matches all whitespace characters including line feed, carriage return, and so on [[: space:]]+
[: Graph:] Matches any character that can be seen and can be printed [[: Graph:]]
[: Xdigit:] Any one hexadecimal number (i.e.: 0-9,a-f,a-f) [[: xdigit:]]+
[: Cntrl:] Any one control character (the first 32 characters in the ASCII character set) [[: Cntrl:]]
[:p rint:] Any one of the characters that can be printed [[:p rint:]]

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.