Simple Introduction to Regular expressions

Source: Internet
Author: User
Tags control characters printable characters



Simple Introduction to Regular expressions

Regular expressions have a lot of application in normal programming, and for anyone who wants to learn programming, regular expressions are a must-have knowledge.

Not much nonsense to say, the following first to do a simple introduction to the regular expression, in the subsequent articles, will be detailed introduction.


One, meta-character

Metacharacters a total of 12: $ () [{? + *. ^ \ |

Metacharacters has a special meaning, and if you want to use its literal value, you must escape it.

such as: \$ \* \ (et cetera )


two, control characters or non-printable characters

\a Alarm

\e exit

\f Page Change

\ nthe line break

\ r Enter

\ t Horizontal Tabulation

\v Vertical Tabulation


Third, shorthand

\d single digit [0-9] ( Note: [] means one of all the characters in the brackets;-is a concatenated character, representing all characters greater than or equal to 0 less than or equal to 9 )

\w letters, numbers, underscores

\s whitespace characters, including spaces, line breaks, tabs

Note: \d \w \s matches characters that are not matched by \d \w \s


Iv. Repetition

Fixed number of times: {8}---repeated 8 times

Infinite times: {n}

0 or more times: {0,} equivalent to *

One or more times: {1,} equivalent to +

0 or one time: {0,1} equivalent to?


Let's look at an example:

The matching time format requires the following:

1. The hours and seconds are two digits, and the milliseconds are represented by three digits.

2. The corresponding range: 00--99

3. The range of minute and second corresponds: 00--59

4. Milliseconds corresponding to the range: 000--999

5. Format as follows: 12:34:56.789


Answer:

1, when the two digits are 0 to 9 one of them, we can be expressed as [0-9][0-9] or [0-9]{2} or \d\d or \d{2}

2, minute and second format is the same, and it is preceded by:, so we represent (: [0-5]\d) {2}

3, milliseconds can be expressed as \d\d\d or \d{3}

Total: \d\d (: [0-5]\d) {2}\.\d{3} note there is a meta-character ".", we take its literal value and need to be escaped.

Verify with Regexbuddy:




Simple Introduction to Regular expressions

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.