Regular Expressions---tools that look hard but are easy to use

Source: Internet
Author: User
Tags expression engine

Regular expressions, we may be intimidated by its complex expressions, but what I'm going to tell you is that as long as you step through it, you will find that the regular expression is not as difficult as you might think , and that the appearance of it will give us a lot of code to write about, and verify that the content is very accurate. Now that you can easily validate your data with regular expressions, what is a regular expression?

1. Definition:

When writing a program or Web page that handles strings, there is often a need to find strings that match certain complex rules. Regular Expressions are the tools used to describe these rules . In other words, the regular expression is the code that records the text rule . the RegExp object is an abbreviation for regular expression (regular expressions).

Note: The regular expression engine typically provides a "test whether a specified string matches a regular expression" method, such as the Regexp.test () method in JavaScript or theRegex.IsMatch () method in. Net . The match here refers to the part of the string that matches the expression rule. If you do not use ^ and $ , for \d{5,12} , the use of such a method can only guarantee that the string contains 5 to 12 consecutive digits , Instead of the entire string, it's 5 to 12 digits .

2. The best way to learn a regular expression is to start with an example and then modify the example yourself to experiment. Here are a few simple examples, and they are described in detail.

Suppose you look in an English novel for hi , you can use regular expressions hi . Typically, a tool that handles regular expressions provides an option to ignore the case (/i), and if this option is selected, it can match hi , hi , HI , HI any of these four cases.

Hi two consecutive characters, such as him , history , high and so on. With hi to find, Hi will also be found. If you want to to find the word hi exactly , we should use \bhi\b .

is a special code prescribed by regular expressions (well, some people call it metacharacters, metacharacter), which represents the beginning or end of a word, that is, the boundary of a word . Although English words are usually delimited by spaces, punctuation marks, or line breaks, \b does not match any of these word-delimited characters, it only matches one position .

If you're looking for a hi, not far behind, follow a Lucy, you should use \bhi\b.*\blucy\b.

used ^ and $ , so the entire string entered is to be used and \d{5,12} to match, which means the entire input must be 5 to 12 digits , so if you enter the QQ number to match the regular expression, then it will meet the requirements.

        $ (function  () {            var value = $ ("#mybutton"). Val ();             var regvalue =/^\d{5,12}$/;             if true {                alert (' verify success! ');            }        })     </script>

Results:

3. character escapes

If you want to find the meta-character itself, such as when you look up , or *, there's a problem: You can't specify them, because they'll be interpreted as something else. Then you have to use \ to cancel the special meaning of these characters. Therefore, you should use \. and \*. Of course, to find \ itself, you also have to use \ \.

For example:deerchao\.net matches deerchao.net,c:\\windows matches C:\Windows.

4. Branch conditions

The branching condition in the expression refers to several rules that should be matched if any of these rules are met, by | Separate the different rules. Don't you understand? Okay, look at the example:

0\d{2}-\d{8}|0\d{3}-\d{7} This expression can match two phone numbers separated by hyphens: one is a three-bit area code, a 8-bit local number (such as 010-12345678), a 4-bit area code, and a 7-bit local number ( 0376-2233445).

5. Grouping

You can use parentheses to specify sub-expressions (also called groupings )

is a simple IP address matching expression. To understand this expression, parse it in the following order: \d{1,3} matches 1 to 3 digit , (\d{1,3}\.) {3} Match three digits plus an English period (this whole is the group , and finally add a one-to-three-digit number ( \d{1,3} ).

6. Anti-righteousness

Today's explanation to here, I hope that we have a harvest, the most important is the regular expression of practice, practice more, the natural use of good (make perfect Yo),

See you next time!

Regular Expressions---tools that look hard but are easy to use

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.