The use of regular expressions in the Shell __ regular expressions

Source: Internet
Author: User

Here are two ways to use the shell's regular expressions. use method One:

#赋值给变量var
read var
#定义regex正则表达式
regex= "[0-9]"

if [$var =~ $regex]];then
    #match
    #do Something
fi

For example, to match the input, the requirement must be a number between 0-9:

#读取输入
Read input

regex= "[0-9]"

if [[$input =~ $regex]];then
    echo ' match '
else
    Echo ' Dismatch "
fi

For method One, a regular expression cannot match if it contains an escape character such as "\d". This will be the second way. use method Two:

Read Var
regex= "[A-z]"

if [' Echo $var | grep-p $regex '];then
    echo ' match '
else
    Echo ' Dismatch "
fi

Attention:
-P parameter:
A regular expression parsing using Perl.

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.