Linux Regular Expression QuickStart

Source: Internet
Author: User

Profile

Some of the basic tools under Linux, such as grep,awk,sed, support regular expressions. It's best to do simple regular expression practice. However, the Kingdom of the regular expression is so vast that it is probably not that simple to master the essence of the regular expression. Many languages, such as perl,php , Java, and so on, have special regular expression support. This book is very comprehensive and meticulous in the presentation of regular expressions, refer here: Mastering regular expressions

Baidu gave the first link is this site, seemingly on the regular expression of the very full, the portal--and regular


Look at some basic meta-characters ~

# The meaning of each symbol in the regular expression represents the repetition of the word, generally the following four symbols, *.+? # matches 0 or 1 characters, (0, 1) *# matches 0 or more characters, (>= 0). # matches 1 characters (= = 1) +# matches one or more characters (>= 1) {m}# character exactly matches m times {m, n}# characters between M and N (inclusive) {m,}# character count above M or m []# matches the range of characters [^]# not within the range ^# with that character $# with that character \d# A number equivalent to [0-9]\d# matches a non-character equivalent to [^0-9]\w# match a letter or number equivalent to [a-za-z0-9]\w# match a non-letter or number equivalent to [^a-za-z0-9]\s# match a newline character equivalent to [\n\t\r\f]. \s# matches a non-newline character equivalent to [^\n\t\r\f]\b# matches the beginning or end of a word

Note that the above four symbols are always mixed, that is, a question mark (?), an asterisk (*), a dot (.), plus (+), because this is not the same as the file wildcard means.


Instance

Let's take a look at two examples and feel the charm of regular expressions

Match IP address ((2[0-4]\d|25[0-5]|[ 01]?\d\d?) \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?]
It looks a bit long, but it's just a little bit of stuff, which(2[0-4]\d|25[0-5]|[ 01]?\d\d?)If it's a cheer, it's (LA). {3} It's understood. This string means that it matches 0 to 255 of any number, of course, 022 is also legal with 0 in front. The back of the cheerleading. means a full stop, except that the period in the regular expression is a meta-character, but first escaped.

Let's analyze It again (2[0-4]\d|25[0-5]|[ 01]?\d\d?), actually consists of three strings, with two in the middle | Symbols are linked together. 2[0-4]\d and [25][0-5] and [01]?\d\d? The question mark indicates that the preceding character can have one or none.


Look at the second one, but I'm still smattering.

Match email \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
In fact, are also the simplest application, join us also use Hehe said\w+ ([-+.] \w+) *Then the original expression has become hehe @ hehe. hehe, it's obviously an email format.

What does that mean, that is, a word (word), denoted by a meta-character (\w), + means that it can be repeated at least once. plus [-+.] Any of the three symbols, asterisks denote parentheses, and can be repeated any time.


The back is a little complicated, and it's still a bit of a scratch.




Linux Regular Expression QuickStart

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.