
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
Awk Regular Expression Commands and Examples
Ultimate Sed Tutorial With Examples

句法(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的语法非常简单,我们只需提供字符串,然后放入运算符,最后一个就是我们要匹配的正则表达式。 我们还将表达式用双括号括起来,如下所示。
[[ STRING =~ REGEX]]
比赛位数 (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

指定行首(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 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表达电子邮件正则表达式。 通常,我们可以对电子邮件使用以下正则表达式模式。
[A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+
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

匹配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
等工具相同。
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
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

翻译自: https://www.poftut.com/how-to-use-regular-expression-regex-in-bash-linux/
bash regex