Regular Expression (1)

Source: Internet
Author: User
Author tag: Regular Expression 2. Development: win32, java,. net CSDN recommended tag: 2. Development: win32 java. net matching rule table simplified result

Previous Article: Regular Expression (2) | next article: generation? What are the best products?


Regular Expression (1) Regular Expression regular expression is a tool used to match strings. For example, if you want to verify whether an email address is valid, you need to find all <Hn>... </Hn> the part of the tag. For example, in delphi, we can use the cxMaskEdit control that supports the regular expression of the regular table to format the batch input.1. match a single characterLiteral matchLiteral match is the match text based on the original text. For example, the string: I have a dream. The regular table contains multiple values: rea. The matched result is: I have a dream.Any (any) MatchingAny match is to match with a wildcard containing a specific meaning, just like the lower limit in SQL, which matches any character. In the regular expression of the positive orders table, this wildcard character is a halfwidth sentence. example: String: I have a dream. normalization formula: r. the result of a's match is: I have a dream. description: If. match any character, but only match. itself, the method is to add the right oblique character before it, just like the escape letter in c, that is \., example: String: I have a dream. normalization table formula :\. the result of the match is: I have a dream. there are still many special characters like this, which will be mentioned later.Ii. Matching of multiple typesMultiple choice and one matchChoose multiple match types as described in the parameter data type in program c statement. They are described in the box. For example, the string: I have a dream, the dream was to be a doctor. positive table formula: t [hbr] e. the matching result is: I have a dream, the dream was to be a doctor.Select multiple matching intervalsFor example, the following table describes the regular expression of the string mydoc1.docmydoc2.docmysheet1.xlsfile5.txt: [a-z] c [1-9] \. Then, the matching result is: success."Anything"MatchIt means to divide... in addition, that is, matching except for the specified characters indicates that the method is to add ^ To the table format. For example, the following formula is used: String: apac1.xlseurope2.xlssam.xls: [ns] a [^ 0-9] \. what is the result of xls match? europe2.xlssam.xlsna1.xls3. metacharactersMetacharacters are the characters used to describe characters in the Regular Expression of the positive table, such as the. Character in front of it. It represents any character. In another example, the square brackets mentioned above are used to describe the matching rule. If you want to represent the condition itself, you need to add a right oblique cursor before it, for example: String: var myArray = new Array ();... if (myArray [0] = 0 ){...} positive table formula: myArray \ [0 \] matched result: var myArray = new Array ();.... if (myArray [0] = 0 ){...} the following table lists several common metacharacters ------------------ [\ B] Return Characters \ n linefeeds \ r return letters \ t Tab \ d (such [0-9]) \ D non-Numeric (equal to [^ 0-9]) \ w letter (equal to [a-zA-Z0-9 _]) \ W non-letter (equal to [^ a-zA-Z0-9 _]) \ s any blank character (equal to [\ f \ n \ r \ t \ v]) \ S any non-blank characters (such as [^ \ f \ n \ r \ t \ v]) ---------------------Iv. Repeat matchingRepeat matching means matching multiple times. The following example shows an example of multiple matches. This example matches all the email addresses in the text: Text: Send personal email to ben@forta.com. for questions about a book use support@forta.com. feel free to send unsolicited email to spam@forta.com (wouldn't it be nice if it were that simple, huh ?). Regular Expression: \ w + @ \ w + \. \ w + match result: Send personal email to ben@forta.com. for questions about a book use support@forta.com. feel free to send unsolicited email to spam@forta.com (wouldn't it be nice if it were that simple, huh ?). Note: In the preceding formula, + indicates that the content in front of the table can be repeated for one or more times. Like +, there are also * And ?, * Indicates that its pre-content can be retained zero or multiple times ,? It indicates that its pre-content can be retained to zero or once.Duplicate matching of the specified number of repeated attemptsTo specify the number of repeated attempts, the formula is {n}. For example, the text is as follows: <body bgcolor = "#336633" TEXT = "# FFFFFF" MARGINWIDTH = "0" MARGINHEIGHT = "0" TOPMARGIN = "0" LEFTMARGIN = "0"> regular form: # [\ d] {6} matched results: <body bgcolor = "#336633" TEXT = "# FFFFFF" MARGINWIDTH = "0" MARGINHEIGHT = "0" TOPMARGIN = "0" LEFTMARGIN = "0">Duplicate matching for specified repeated data intervalsThe formula is {n, m} {n,}. For example, the formula is as follows: Text:/8/0310-6-20042/2: \ d {1, 2} [-\/] \ d {1, 2} [-\/] \ d {2, 4} matched results: /8/0310-6-20042/2/201-01-01Identification of Multiple Levels--------------**? ++? {N ,}{ n ,}? --------------5. start and end charactersStart and end with a single wordLet's look at the example below. Text: The cat scattered his food all over the room. formula: cat match result: The cat scattered his food all over the room. Let's look at The example below. Text: The cat scattered his food all over the room. result of matching of positive table formula: \ bcat \ B: The cat scattered his food all over the room. for the two examples above, we can see that \ B is a start and end character, indicating that there are no other non-null characters before or after it, matches a complete word. Let's look at two more examples to better understand \ B. Text: The captain wore his cap and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel. positive table formula: \ bcat match result: The captain wore his cap and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel. text: The captain wore his cap and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel. result of cat \ B matching: The captain wore his cap and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel.Starting and endingIf you want to match the jave Statement on behalf of a single row, you can take the following example. Text: function doSpellCheck (form, field) {// Make sure not empty if (field. value = '') {return false;} // Init var windowName = 'shellwindow'; var spellCheckURL = 'spell. cfm? Formname = comment & fieldname = '+ field. name ;... // Done return false;} positive table formula: ^ \ s * // [\ n \ r] * $ matched result: function doSpellCheck (form, field) {// Make sure not empty if (field. value = '') {return false;} // Init var windowName = 'shellwindow'; var spellCheckURL = 'spell. cfm? Formname = comment & fieldname = '+ field. name ;... // Done return false;} Description: As shown in the previous example, metacharacters ^ if it appears in [], it indicates no. Otherwise, it indicates the start of the line; metacharacters $ indicate the end of the line.

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.