72. Use the shell regular expression to determine the IP address

Source: Internet
Author: User

1. Expression writing: [2]

Regex_ip = "(2 [0-4] [0-9] | 25 [0-5] | 1 [0-9] [0-9] | [1-9]? [0-9]) (\. (2 [0-4] [0-9] | 25 [0-5] | 1 [0-9] [0-9] | [1-9]? [0-9]) {3 }"

It means (0-255) (point 0-255) 3 times

Echo "123.1.1.1" | grep-e "$ regex_ip"

Method 2:

Read IP

Echo $ IP | awk-F '. ''{If ($1> 256 | $1 <0) | ($2> 256 | $2 <0) | ($3> 256 | $3 <0) | ($4> 256 | $4 <0) Print $0, "is incorrect "}'

2. Notes

Because the extended regular | and? So grep must add-e

Reference

[1] Regular Expression

Http://www.cnblogs.com/mydomain/archive/2011/08/27/2155340.html

Http://www.cnblogs.com/mydomain/archive/2010/10/17/1853661.html

[2] explains the reason for this regular expression step by step

Http://blog.chinaunix.net/space.php? Uid = 20231929 & Do = Blog & id = 2747894

[3] common Regular Expressions

Http://hi.baidu.com/puma525/blog/item/8251fbd39ba76dcba8ec9a4d.html

[4] differences between bre and ere

Http://www.4ucode.com/Study/Topic/127403

Http://blog.csdn.net/cheungjustin/article/details/5897982

Ere modified some of Bre syntax and added the following syntax symbols:

? -Match at most once (zero or one match)

+-Match at least once (one or more times ).

|-Or operation. both the left and right operands can be considered as a subexpression.

At the same time, ere cancels the escape character reference syntax for the subexpression "()" and number of times matching "{m, n}" syntax symbols. When these two syntax symbols are used,You do not need to add escape characters..

At the same time, ere also removes the ability of subexpressions with non-regular semantics to forward references.

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.