Linux Basics: The usage of regular expressions in the shell

Source: Internet
Author: User

In Linux systems, regular expressions (regularexpression) are powerful tools for finding, replacing, deleting, and so on for every Linux user, through the arrangement of special characters.
In existing systems, regular expressions are also divided into basic regular expressions (grep), extending regular Expressions (egrep). Before formally understanding regular expressions, understand some of the relevant character concepts, which have special meanings and usages in regular expressions.

For the underlying regular expression grep, each special character has a different match:

1. Character Matching:


. : Matches any single character
[]: matches any single character within the range, such as [0-9],[a-z],[a-z] ...
[^]: matches any single character outside the range
[:d Igit:]: Matches any single number, 0-9
[: Lower:]: matches any single lowercase letter, a-Z
[: Upper:]: Matches any single capital letter, A-Z
[: Alpha:]: Matches any single letter, including case a-z,a-z
[: Alnum:]: Matches any single character, including a-z,a-z,0-9
[: Space:]: matches any character that produces whitespace

2. Number of times match: Used to specify the number of occurrences of the character, to limit the number of characters before it appears;

*: matches the preceding character any time (0,1 or more);
grep "X*y":
XXXYABC: Match 3 times
YABC: Match 0 times
Abcxy: Match Once
Abcy: Match 0 times

. *: Matches any character of any length;

\+: Matches the preceding character at least 1 times;
grep "X\+y":
XXXYABC: Match 3 times
YABC: Not matched
Abcxy: Match Once
Abcy: Not matched

\?: matches the preceding 0 or 1 times, that is, the preceding character is optional;
grep "X\?y":
XXXYABC: Match 1 times
YABC: Match 0 times
Abcxy: Match 1 times
Abcy; Match 0 times

\{m\}: The preceding character appears m times and M is a nonnegative integer;
grep "X\{2\}y":
XXXYABC: Match
YABC: Not matched
Abcxy: Not matched
Abcy: Not matched

\{m,n\}: The preceding character appears m times and M is a nonnegative integer; [M,n]
\{0,n\}: Up to n times;
\{m,\}: at least m times;
\{m,n\}: The preceding character appears at most n times, at least m times

3. Position Lock: Limit the use of mode search text, limit the pattern to match the text can only appear in the target text specific location;

^: The beginning of the line lock; for the leftmost mode, ^pattern
$: End of line lock; for the right-most of the pattern, pattern$
^pattern$: To make PATTERN exactly match a whole line;
^$: Empty line;
^[[:space:]]*$: A line that starts with a space and ends with any character

Word locking: A continuous character (string) consisting of non-special characters is called a word;

\< or \b: The word head is locked for the left side of the word pattern, formatted as \<pattern, \bpattern
\> or \b: The ending lock for the right side of the word pattern, formatted as PATTERN\&GT; pattern\b
\<pattern\>: Word lock;

Here are some examples of how to use the relevant usage:

This article is from the "11283159" blog, please be sure to keep this source http://11293159.blog.51cto.com/11283159/1749739

Linux Basics: The usage of regular expressions in the shell

Related Article

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.