Learn regular expressions (high efficiency) _ Regular expressions in real-world examples

Source: Internet
Author: User
Tags regular expression

Introduction of regular expressions

Regular expressions, also known as formal representations, general representations. (English: Regular Expression, often abbreviated as regex, RegExp, or re) in code, a concept of computer science. A regular expression uses a single string to describe and match a sequence of rules that conform to a certain syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that conforms to a pattern.

Recently, the whole study of the regular expression of knowledge, found or in the example of learning efficiency is higher, then share the regular expression of the classic examples and related knowledge points of the summary.

Example 1: Matching 5-12-digit numbers: ^\d{5,12}$

First, we introduce two special symbols, ^ for the beginning of the string, and $ for the end of the string, which is used to qualify the matching range. Next, \d is used to match a number, which can be followed by a {}, where the number is entered. When you enter a number, such as {3}, the number is matched 3 times, and when you enter two digits, as in the example, the match 5-12 times.

Example 2: Match a word beginning with the letter A: \ba\w*\b

In regular expressions, you can use characters directly to match, such as using a to indicate a pattern that matches a letter. \b Represents the edge of a character. \w indicates that all characters except spaces are matched. In regular expressions, you can use a number of symbols to represent how many times, where the * represents a match 0-n, + indicates a match 1-n,? Indicates that the match is 0 or 1 times. So in the example above, \w* represents a followed by 0 or more characters. So the whole pattern matches the characters that start with a.

Example 3: Matching landline phone number: \ (? 0\d{2}[)-]?\d[8]

This regular expression matches a phone number such as the one in the form (010) 88886666 or 022-22334455. The first thing to note is that because (?) symbols have special meanings in regular expressions, you need to turn around when using real symbols of this type. In this example, the first \) is indicated (0 or 1 times). Next is the area code, because the area code is 3 digits and the number at the beginning must be 0, so using 0\d{2} can perfectly match this requirement. And then the use of []. [] can be placed in multiple values, indicating that the next match is one of these characters. For example, here's [)-]? means to match an opening parenthesis, space, or-one of the 0 or 1 times. Finally, using the \d{8} to match the 8-digit number will meet the requirements.

Example 4: Match the front part of the word with ing ending: \b\w+ (? =ing\b)

This uses a 0-wide assertion, and the (? =exp) in the 0-wide assertion represents the previous part of the request that matches the latter. The (? =ing\b) match in this example is the character to end with Ing.

Above through four examples to introduce the regular expression of knowledge, very good, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.