My first regular expression

Source: Internet
Author: User

Although famous for a long time, but, after all, is not the opportunity, every time is a shallow, and even did not write a line of regular expression. Today, I finally wrote my first regular expression, remembering:

\s/udisk/[1-9]*

Above this pattern is mainly for, in a long string, find the string "/udkisk/xx", where xx represents a string of numbers, the length of the string is not required.

Explain the above sections:

1) "\s", which represents a white space character, matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v]. Usually as a starting character for a string. Such characters, in regular expressions called metacharacter, Chinese translation do meta-characters . The metacharacters that represents "one character" are also:

-"\b", which matches the beginning or end of a word in English, can be a space, punctuation, line wrapping. But, in fact, it just matches a position, just to find the beginning or end of a word.

-"." to match any character outside the line break.

-"\d", matching a number. equivalent to [0-9] represented by a set.

-"\w", matching a letter, or number, or underscore, or kanji.

-"Range [1-9]" or "set [FSEFSFD]", in fact both of the corresponding English words can be set, here is the match range, or a character within the collection.

2) So, using the above metacharacters to take my first regular expression apart, is "\s" + "/udisk/" + "[1-9]*", so we want to match the string is "space" + "/udisk/" + "number" + "number" + ... + "Number". The "*" here means that the previous meta-character will appear 0 or more times.

3) The above "*" is called a qualifier , that is, the number of occurrences of the limit. Qualifiers in regular expressions include the following:

-"*", appearing 0 or infinitely multiple times.

-"+", appearing 1 times or infinitely multiple times. At least 1 times.

-"?", appearing 0 or 1 times.

-"{n}", which occurs n times.

-"{n,}", which occurs n or more times.

-"{n,m}", which appears at least n times, up to M times.

4) We can also enclose a string of meta-characters and qualifiers (or other symbols) in parentheses, and then treat the expression as a meta-character, which is called a group. For example, (\w+\.) {2}\w+, which means "xxx.xxx.xxx", where xxx is any number of letters.

This is just a part of the regular expression getting started, so ... and to refuel. In addition, Bash uses the regular expression of the three sharp weapon, also try to master:Grep,sed,awk.

My first regular expression

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.