Selection and Grouping

Source: Internet
Author: User
Tags end expression regular expression string

Select Allow the ' | ' character to be selected in two or more candidates. By extending the regular expression of the chapter title, you can extend it to an expression that applies not only to chapter headings. However, this is not as direct as you might think. When you use a selection, the most likely expression on each side of the ' | ' character is matched. You might think that the following JScript and VBScript expressions match the ' Chapter ' or ' section ' at the beginning and end of a line and followed by one or two digits:

/^Chapter|Section [1-9][0-9]{0,1}$/"^Chapter|Section [1-9][0-9]{0,1}$"

Unfortunately, the real situation is that the regular expression shown above either matches the word ' Chapter ' at the beginning of a line or the ' section ' followed by any number at the end of a line. If the input string is ' Chapter 22 ', the expression above will only match the word ' Chapter '. If the input string is ' section 22 ', the expression will match ' section 22 '. But this result is not our purpose here, so there has to be a way to make regular expressions more responsive to what you want to do, and you do have this approach.

You can use parentheses to limit the range of selections, which means that the selection applies only to the two words ' Chapter ' and ' section '. However, parentheses are also difficult to handle, because they are also used to create subexpression, and some of the content is described later in the section on the subexpression. By taking the regular expression shown above and adding parentheses in place, you can make the regular expression match either ' Chapter 1 ' or ' section 3 '.

The following regular expression uses parentheses to form a group of ' Chapter ' and ' section ' so that the expression works correctly. For JScript:

/^(Chapter|Section) [1-9][0-9]{0,1}$/

For VBScript:

"^(Chapter|Section) [1-9][0-9]{0,1}$"

These expressions work correctly, but produce an interesting by-product. In ' chapter| Section ' placed parentheses on both sides establishes the appropriate grouping, but also causes one of the two matching words to be captured for future use. Because there is only one set of parentheses in the expression shown above, there can be only one captured Submatch. You can reference this child match using the submatches collection of VBScript or the $1-$9 property of the RegExp object in JScript.

Sometimes capturing a child match is desirable and sometimes not desirable. In the example shown in the description, the real thing to do is to use parentheses to group the selection between the word ' Chapter ' or ' section '. You do not want to refer to the match later. In fact, do not use unless you really need to catch a child match. This regular expression is more efficient because it does not take time and memory to store those child matches.

You can use '?: ' in front of the regular expression pattern parenthesis to prevent the match from being stored for future use. The following modifications to the regular expression shown above provide the same functionality for exempting the child-matching store. For JScript:

/^(?:Chapter|Section) [1-9][0-9]{0,1}$/

For VBScript:

"^(?:Chapter|Section) [1-9][0-9]{0,1}$"

In addition to the '?: ' metacharacters, there are also two non-capture meta characters used to refer to the matching of the pre-search . A forward lookup, with a? =, matches the search string at any position that begins to match the regular expression pattern within the parentheses. A negative lookup, using '?! ', matches the search string at any position that does not begin to match the regular expression pattern.

For example, suppose you have a document that contains references to Windows 3.1, Windows 95, Windows 98, and Windows NT. Further assume that you need to update the document by looking for all references to Windows 95, Windows 98, and Windows NT, and change those references to Windows 2000. You can use the following JScript regular expression, which is a forward check to match Windows 95, Windows 98, and Windows NT:

/Windows(?=95 |98 |NT )/

To do the same thing in VBScript, you can use the following expression:

"Windows(?=95 |98 |NT )"

When a match is found, the next matching search is initiated immediately after the matching text (not including the characters used in the search). For example, if the expression shown above matches the ' Windows 98 ', the lookup will continue after ' windows ' instead of ' 98 '.



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.