bash regex
Linux bash provides a lot of commands and features for Regular Expressions or regex.
grep
,
expr
,
sed
and
awk
are some of them. Bash also have
=~
operator which is named as
RE-match
operator. In this tutorial we will look
=~
operator and use cases. More information about regex command cna be found in the following tutorials.
Linux bash為正規表達式或正規表達式提供了許多指令和功能。
grep
,
expr
,
sed
和
awk
就是其中一些。 Bash還具有
=~
運算符,稱為
RE-match
運算符。 在本教程中,我們将檢視
=~
運算符和用例。 在以下教程中可以找到有關正規表達式指令cna的更多資訊。
Introduction to Linux Grep Command With Examples
Linux Grep指令簡介和示例
Awk Regular Expression Commands and Examples
AWK正規表達式指令和示例
Ultimate Sed Tutorial With Examples
帶有示例的Ultimate Sed教程
句法(Syntax)
Syntax of the bash rematch is very easy we just provide the string and then put the operator and the last one is the regular expression we want to match. We also surround the expression with double brackets like below.
bash rematch的文法非常簡單,我們隻需提供字元串,然後放入運算符,最後一個就是我們要比對的正規表達式。 我們還将表達式用雙括号括起來,如下所示。
比賽位數 (Match Digits)
In daily bash shell usage we may need to match digits or numbers. We can use bash regex operator. We will state numbers with
[0-9]
like below. But keep in mind that bash regex can be fairly complicated in some cases. In this example we will simple match given line for digits
在日常的bash shell使用中,我們可能需要比對數字或數字。 我們可以使用bash regex運算符。 我們将用
[0-9]
表示數字,如下所示。 但是請記住,bash regex在某些情況下可能會相當複雜。 在此示例中,我們将簡單地比對給定行的數字
digit="ismail poftut 12345"
if [[ $digit =~ [0-9] ]]; then
echo "$digit is a digit"
else
echo "oops"
fi
Match Digits 比賽位數
指定行首(Specify Start Of Line)
In previous example we have matched digits in the whole line. This is not case some times. We may need to match from start of the line with digits of other character type. We can use
^
to specify start of the line. In this example we will match line which starts with
123
. As we can see it didn’t match.
在前面的示例中,我們在整行中都比對了數字。 有時候不是這種情況。 我們可能需要從行首開始将其與其他字元類型的數字進行比對。 我們可以使用
^
指定行的開始。 在此示例中,我們将比對以
123
開頭的行。 如我們所見,它不比對。
digit="ismail poftut 12345"
if [[ $digit =~ ^123 ]]; then
echo "$digit is a digit"
else
echo "oops"
fi
Specify Start Of Line 指定行首
指定行尾(Specify End Of Line)
We can also specify the end on line. We will use
$
to specify end of line. We will match line which ends with any digit.
我們也可以指定結束行。 我們将使用
$
指定行尾。 我們将比對以任何數字結尾的行。
digit="ismail poftut 12345"
if [[ $digit =~ [0-9]$ ]]; then
echo "$digit is a digit"
else
echo "oops"
fi
比對電子郵件 (Match Email)
Digit patterns are easy to express but how can we express email regex in bash. We can use following regex pattern for emails generally.
數字模式很容易表達,但是我們如何用bash表達電子郵件正規表達式。 通常,我們可以對電子郵件使用以下正規表達式模式。
We will ommit suffixes like
com
,
net
,
gov
etc. because there is a lot of possibilities. As we know
@
is sitting between username and domain name.
我們将省略諸如
com
,
net
,
gov
等字尾,因為存在很多可能性。 衆所周知,
@
位于使用者名和域名之間。
email=$1
if [[ "$email" =~ "^[A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+<b>\.</b>[A-Za-z]{2,4}$" ]]
then
echo "This email address looks fine: $email"
else
echo "This email address is flawed: $email"
fi
Match Email 比對電子郵件
比對IP位址(Match IP Address)
IP address is another type of important data type which is used in bash and scripting. We can match IP addresses by using bash regex. We will use following regex pattern which is the same with tools like
grep
and others.
IP位址是bash和腳本中使用的另一種重要資料類型。 我們可以使用bash regex來比對IP位址。 我們将使用以下正規表達式模式,該模式與
grep
等工具相同。
ip=$1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "Looks like an IPv4 IP address"
elif [[ $ip =~ ^[A-Fa-f0-9:]+$ ]]; then
echo "Could be an IPv6 IP address"
else
echo "oops"
fi
Match IP Address 比對IP位址 LEARN MORE Linux tr Command Tutorial With Examples 通過示例了解更多Linux tr指令教程
翻譯自: https://www.poftut.com/how-to-use-regular-expression-regex-in-bash-linux/
bash regex