Locator character
Until now, the examples you see are only looking for chapter headings that appear anywhere. Any occurrence of a string ' Chapter ' followed by a space and a number may be a true chapter title, or it may be a cross-reference to other chapters. Because true chapter headings always appear at the beginning of a line, you need to design a method to find only the headings instead of looking for cross-references.
This feature is provided by the locator character. A locator can fix a regular expression at the beginning or end of a line. You can also create regular expressions that appear only within a word or only at the beginning or end of a word. The following table contains a list of regular expressions and their meanings:
Character description
^ matches the start position of the input string. If you set the Multiline property of the RegExp object,
^ also matches the position after ' or '.
$ matches the end position of the input string. If you set the Multiline property of the RegExp object,
$ also matches the position before ' or '.
Matches a word boundary, which is the position between the word and the space.
B matches a non word boundary.
Qualifiers cannot be used on the locator. An expression such as ' ^* ' is not allowed because there are no consecutive positions in front of or behind a newline character or word boundary.
To match the start of a line of text, use the ' ^ ' character at the beginning of the regular expression. Do not confuse the syntax of ' ^ ' with the syntax in the bracket 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.
To use a locator when finding chapter headings, the following Visual Basic scripting Edition Regular Expressions match the chapter headings at the beginning of a line with up to two digits:
/^chapter [1-9][0-9]{0,1}/
The regular expressions for the same functionality in VBScript are as follows:
"^chapter [1-9][0-9]{0,1}"
A true chapter title not only appears at the beginning of a line, but only in this line, so it must also be at the end of a line. The following expression ensures that the specified match matches only the chapter and does not match the cross-reference. It is accomplished 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 use:
"^chapter [1-9][0-9]{0,1}$"
Matching word boundaries is a little different, but it adds a very important function to regular expressions. A word boundary is the position between a word and a space. A non word boundary is any other location. The following Visual Basic scripting Edition expression will match the first three characters of the word ' Chapter ' because they appear after the word boundary:
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.