Regular Expression tutorial-subexpression Usage Analysis, regular expression

Source: Internet
Author: User

Regular Expression tutorial-subexpression Usage Analysis, regular expression

This document describes how to use a subexpression in the regular expression tutorial. We will share this with you for your reference. The details are as follows:

Note: In all examples, regular expression matching results are included between [and] in the source text, and some examples are implemented using java. If the regular expression is used in java, it will be described in the corresponding area. All java examples have passed the test under JDK1.6.0 _ 13.

I. Problem Introduction

First, let's look at an example. Some phrases such as Windows 2000 are composed of multiple words, but they are actually a whole. non-line breaks (non-breaking space) can be used in HTML pages) make it display on one line in the browser. Now we will match multiple such spaces:

Text: Your operation systemis Windows 2000.

Regular Expression: nbsp; {2 ,}

Result: Your operation systemis Windows 2000.

Analysis: here we use the pattern to match two or more non-wrap spaces, but we can see from the result that nothing is matched, because nbsp; {2 ,} this mode can only match text that starts with nbsp and has two or more consecutive semicolons.

Because the repeated match mentioned above is repeated multiple times next to the character before the repeated match metacharacters. However, if we want to perform multiple matches on a string, what should I do?

Ii. subexpressions

From the above, we will introduce the subexpression. A subexpression is a part of a large expression. The purpose of dividing an expression into multiple subexpressions is to use these subexpressions as an independent element. The subexpression must be enclosed by (and. Therefore, the regular expression in the preceding example should be written as (nbsp;) {2 ,}.

Let's look at a regular expression matching a valid year:

Text: 1988-11-13

Regular Expression: (19 | 20) \ d {2}

Result: [1988]-11-13

Analysis: In this example, to exclude meaningless years, limit the first two digits of the year to 19 or 20. | it is a regular expression or operator. 19 | 20 must be placed in a subexpression, that is, (19 | 20). Otherwise, it can only match the year starting with 20,

3. subexpression nesting

Child expressions allow nesting and multi-layer nesting. There is no theoretical limit on nested layers.

In expression (A) (B (C), the following subexpressions exist:

1 (A) (B (C )))

2 ()

3 (B (C ))

4 (C)

A total of four, 0th always represent the entire expression. In the backtracing reference, we will introduce the reference of a subexpression through \ n (n is the number of the subtable type.

For examples of subexpression nesting, see the regular expression that matches IPV4 addresses.

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

I hope this article will help you learn regular expressions.

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.