By Alain Francois
Regular Expressions are special characters that help search data and match complex patterns using placeholder characters called operators, such as Joker or _Wild-card_. They are commonly called regex (regular expression) and represent patterns of character strings. They can be found in many languages, such as PHP, MySQL, and JavaScript.
Dome commons regular expressions that can be used to match some of your filter expectations are listed below:
^
matches the beginning of a line or string$
matches the end of a line or string*
matches zero (0) or more occurrences of an element. You should also be aware that the result can show those with the specified element. This means a*
can give no occurrence, such as aa
or aeb
.?
matches zero (0) or one (1) occurrence.
(period) matches any character+
matches one (1) or more occurrences of an element. This means a+
should be a
, aa
, or aaa
. You can see that it is different from the *
.left|right
will succeed if the right or left pattern side matches.[]
is used to match one of the characters in the brackets. You can specify a range when using -
inside the brackets.
[a-z]
: matches a single lowercase letter[A-Z]
matches a single uppercase letter[0-9]
: matches any number()
It is used to match or search for a set of regular expressions.This guide uses an Alibaba Cloud ECS Linux instance. If you don’t have an Alibaba Cloud account already, you can sign up for a Free Trial.
You can match a group of characters by enclosing them inside the brackets.
Let's filter all the lines containing mai
or man
in the /etc/passwd
file:
$ grep '^daemon' /etc/passwd
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
$ sed -n '/ma[in]/p' /etc/passwd
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
We can also filter all the lines containing syn
, syo
, syp
down to syt
:
$ grep 'sy[n-t]' /etc/passwd
$ sed -n '/sy[n-t]/p' /etc/passwd
Anchors are meta-characters that allow you to specify where in the line the match must be found.
Let's try to list all the lines starting with the string daemon
in the /etc/passwd
file:
$ grep '^daemon' /etc/passwd
$ sed -n '/^daemon/p' /etc/passwd
Now, let’s list all the lines ending with the string bash
in the /etc/passwd
file:
$ grep 'bash$' /etc/passwd
$ sed -n '/bash$/p' /etc/passwd
You can use the .
operation to match any single character:
$ sed -n '/sys..g/p' /etc/passwd
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
You can also filter all the lines containing all occurrences of specified words or strings:
$ grep -E 'syslog|bash' /etc/passwd
root:x:0:0:root:/root:/bin/bash
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
franck:x:1000:1000:,,,:/home/franck:/bin/bash
Using *
we can match up to zero or more occurrences of the character of the string:
# grep 'mes*age' /etc/passwd
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
AiTreat: Robot Masseuse Performs Customized Physiotherapy Treatments
1,029 posts | 252 followers
Followdigoal - December 11, 2019
Alibaba Clouder - May 14, 2018
Alibaba Cloud Native Community - January 6, 2023
Alibaba Cloud Native Community - November 4, 2024
Alibaba Cloud Native Community - November 1, 2024
Alibaba Cloud Community - June 28, 2023
1,029 posts | 252 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreMore Posts by Alibaba Cloud Community