Php-pcre Regular expression anchors and periods

Source: Internet
Author: User

Anchor

Outside of a character class, in the default match mode, ^ is an assertion that asserts that the current match is at the beginning of the target string. Inside a character class, ^ indicates that the character described in this character class is reversed (see below).

^ is not necessarily the first character of a pattern, but if it is in an optional branch, it should be the first character of the branch. If all selection branches start with a ^, that is, if the pattern is limited to the beginning of the target, it is called a "fastening" mode. (There are also other ways to construct the fastening mode)

$ is used to assert that the current match point is at the end of the target string, or when the target string ends with a newline character at the current match point at the line break position (by default). $ does not have to be the last character of the pattern, but if it is in an optional branch, it should be at the end of the branch. $ has no special meaning in the character class.

The meaning of $ can be changed to match only the end of the string by setting pcre_dollar_endonly at compile or match time. This does not affect the behavior of the \z assertion.

The meaning of the ^ and $ characters changes when the Pcre_multiline option is set. When in this case, they match the characters that follow and precede each newline character, and also match the start and end of the target string. For example, the pattern/^abc$/successfully matches the target string "DEF\NABC" in multiline mode, but not normally. Therefore, since all the optional branches start with ^, this becomes the fastening mode in single-line mode, however in multi-line mode, this is non-fastening. The pcre_dollar_endonly option is invalidated after the pcre_multiline is set.

Note: Escape sequences such as \a, \z, \z, and so on can be used in any mode to match the starting and ending positions of the target string. And if all branches of the pattern start with \a, it is also fastened, regardless of whether the pcre_multiline is set.

Period

Outside the character class, a period in a pattern matches any character in the target string, including nonprinting characters, but (by default) does not include line breaks. If Pcre_dotall is set, the period matches the line break. The processing of periods is not associated with the processing of ^, $, and their only relationship is that they all involve newline characters. Periods do not have any meaning in the character class.

\c can be used to match a single byte, meaning that a period can match multibyte characters in UTF-8 mode.

  • 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.