VBS Tutorial: Introduction to regular expressions-selection and grouping

Source: Internet
Author: User

Select and group

Select the '|' character to select two or more candidate items. By extending the regular expression of a Chapter title, you can expand it to an expression not only applicable to the Chapter title. However, this is not as straightforward as imagined. When you use the selection option, the '|' character is matched with the most likely expression on each side. You may think that the following Visual Basic Scripting Edition and VBScript expressions will match the 'Chapter 'or 'secte' at the beginning and end of a row followed by one or two numbers ':

/^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 row, or matches the 'core' at the end of a row followed by any number '. If the input string is 'Chapter 22 ', the above expression will only match the word 'Chapter '. If the input string is 'section 22', the expression matches 'section 22 '. However, this result is not our purpose here, so there must be a way to make the regular expression easier to respond to, and there is indeed this method.

You can use parentheses to limit the selection range. That is to say, it is clear that the selection only applies to the two words 'Chapter 'and 'section '. However, parentheses are also hard to handle because they are also used to create subexpressions. Some content will be described later in the section about subexpressions. By using the regular expression shown above and adding parentheses at the appropriate position, you can make the regular expression match 'Chapter 1' or 'section 3 '.

The following regular expression uses parentheses to form a group of 'Chapter 'and 'section', so that the expression can work correctly. For Visual Basic Scripting Edition:

/^(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. An appropriate group is created by placing parentheses on both sides of 'Chapter | Section ', which also results in the capturing of one of the two words to be matched for future use. Since the expression shown above only contains a set of parentheses, only one capturedSubmatch. You can use VBScriptSubmatchesSet or in Visual Basic Scripting EditionRegExpObject$1-$9Attribute to reference this child match.

Sometimes it is expected to capture a child match, but sometimes it is not expected. In the example shown in the description, you really want to use parentheses to select a group between the words 'Chapter 'or 'section. You do not want to reference this match later. In fact, do not use it unless you really need to capture the child match. Because you do not need to spend time and memory to store those child matches, this regular expression is more efficient.

You can use '? . The following modifications to the regular expression shown above provide the same function of saving sub-matching storage. For Visual Basic Scripting Edition:

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

For VBScript:

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

Except '? : 'Metacharacters. Two non-capturing metacharacters are also calledPre-Check. One is forward pre-query, use? = Indicates that the search string is matched at any position that begins to match the Regular Expression Pattern in parentheses. One is negative pre-query. Use '?! 'Indicates that the string is matched at any position that does not match the regular expression pattern.

For example, assume that there is a document that references Windows 3.1, Windows 95, Windows 98, and Windows NT. Further assume that you need to update this document by searching for all references to Windows 95, Windows 98, and Windows NT and changing these references to Windows 2000. You can use the following Visual Basic Scripting Edition regular expression, which is a forward pre-query to match Windows 95, Windows 98, and Windows NT:

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

The following expressions can be used to perform the same matching in VBScript:

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

After a match is found, the matched text (excluding the characters used in the pre-query) starts to search for the next match. For example, if the expression shown above matches 'windows 98 ', the search will continue from '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.