Regular Expressions Daily collection collation (simple and practical) _ Regular expressions

Source: Internet
Author: User

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.

The function of the regular expression is very powerful, the nonsense does not say much, give everybody to share the content of the reorganization directly.

One: basic matching characters:

\d matching number eg: ' 5\d0 '-------> ' 580 '

\w matching letters or numbers eg: ' \d\w\w '---------> ' 8zh '

. Matches any character except a newline character, eg: ' en. '-----------> ' zh& '

\s whitespace character (tab) or space

Two: Match character length:

* represents any character (including 0)

+ represents at least one character

? Represents 0 or 1 characters

{n} represents n characters

{N,m} represents n-m characters

********************************************************

Exercises together: \d{3}\s+\d{3,8};

The answer here is 2333: The match is made up of 3 digits immediately followed by at least one space or a blank character immediately following 3 to 8 digits;

********************************************************

<<<<< added: "Such special characters as '-' need to be translated in order to match oh like this ' \-' >>>>>

Three: But if this is a simple match, it's a little bit simpler, and we need more accurate matching methods for complex matches:

[0-9a-za-z\_] can match a number or a letter or an underscore, the test: ' 0 ', ' a ', ' Z ', ' _ ' can match, so that the expression of the medium is a possible or can be the composition of the way;

[0-9a-za-z\_]+ can match a string consisting of at least one number or letter or underscore;

[A-za-z\_] [0-9a-za-z\_]* can match the beginning of a letter or underscore followed by any number or letter or underscore;

[A-za-z\_] [0-9a-za-z\_] {0,19} limit length of 1 to 20, why 1 to 20, the beginning of a bit immediately after the string limit to 0 to 19, so the maximum length limit in 20;

a| B can match A or b. [J|j]aina can match Jaina or Jaina;

^ expression must begin with a character, eg: ^\d must begin with a number

$ means must end with a character, eg: \d$ must end with a number, notice the way they are expressed ^ is placed in front of the character, $ is placed behind the character

Four: JS in the verification method:

var reg= ' \d{3}\s+\d{3,8} ';
var tel= ' 010 123456 ';
Console.log (Reg.test (tel));//true

The above is a small series to introduce regular expression of routine collection (simple and practical), hope to help everyone, 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.