VBS Tutorial: Regular Expression introduction-positioning Operator

Source: Internet
Author: User

Operator

Until now, all the examples we see are looking for chapter titles that appear anywhere. Any character string 'Chapter 'followed by a space and a number may be a real Chapter title or a cross reference to other chapters. Because the title of a chapter always appears at the beginning of a row, you need to design a method to only search for the title rather than cross reference.

The locator provides this function. A regular expression can be fixed at the beginning or end of a row. You can also create a regular expression that appears only within or at the beginning or end of a word. The following table lists the regular expressions and their meanings:

Character Description
^ Matches the start position of the input string. IfRegExpObjectMultilineProperty, ^ matches the position after '\ n' or' \ R.
$ Matches the end position of the input string. IfRegExpObjectMultilineAttribute, $ also matches the position before '\ n' or' \ R.
\ B Match A Word boundary, that is, the position between a word and a space.
\ B Match non-word boundary.

The delimiters cannot be used. Because there are no consecutive positions before or after a linefeed or word boundary, expressions such as '^ *' are not allowed.

To match the beginning of a line of text, use the '^' character at the beginning of the regular expression. Do not mix the '^' syntax with the syntax in the brackets expression. Their syntax is fundamentally different.

To match the text at the end of a line of text, use the '$' character at the end of the regular expression.

The following Visual Basic Scripting Edition Regular Expression matches the title of a chapter with a maximum of two numbers at the beginning of a row:

/^Chapter [1-9][0-9]{0,1}/

The regular expressions with the same functions in VBScript are as follows:

"^Chapter [1-9][0-9]{0,1}"

The title of a real chapter appears not only at the beginning of a row, but also at the end of a row. The following expression ensures that the specified match matches only the chapter but does not match the cross reference. It is implemented by creating a regular expression that matches the start and end positions of only one line of text.

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

For VBScript:

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

Matching word boundary is slightly different, but it adds a very important function to the regular expression. A word boundary is the position between a word and a space. Non-word boundary is any other position. The following Visual Basic Scripting Edition expressions match the first three characters of the word 'Chapter 'because they appear after the word boundary:

/\bCha/

For VBScript:

"\bCha"

Here, the position of the '\ B' operator is critical. If it is located at the beginning of the string to be matched, it searches for the match at the beginning of the word; if it is located at the end of the modified string, it looks for the match at the end of the word. For example, the following expression matches the 'ter 'in the word 'Chapter' because it appears before the word boundary:

/ter\b/

And

"ter\b"

The following expression matches 'apt 'because it is located in the middle of 'Chapter', but does not match 'apt 'in 'aptitude ':

/\Bapt/

And

"\Bapt"

This is because 'apt 'appears in the non-word boundary position in the word 'Chapter', while in the word 'aptitude', it is at the word boundary position. The position of the non-word boundary operator is not important because the match is irrelevant to the start or end of a word.

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.