More meta-characters

Source: Internet
Author: User
Tags first string

The remaining part of the meta-character to be discussed is the 0-wide qualifier (zero-width assertions). They do not make the engine faster at handling strings, but they do not correspond to any characters at all, but simply succeed or fail. For example, \b is a qualifier (assertions) that locates the current position at the boundary of a word, and this position is not changed by the \b at all. This means that the 0 wide qualifier (zero-width assertions) will never be duplicated, because if they match at a given position once, they can obviously be matched countless times.

|

An option, or an "or" operator. If a and B are regular expressions, a| B matches any string that matches the "a" or "B". is very low in order to be able to run properly when you have multiple strings to choose from. crow| Servo will match "Crow" or "Servo" instead of "Cro", a "w" or an "S", and "Ervo".

To match the letter "|", you can use |, or include it in a character class, such as [|].

^

Matches the beginning of the line. Unless you set the MULTILINE flag, it only matches the beginning of the string. In MULTILINE mode, it can also directly match each line break in a string.

For example, if you only want to match the word "from" at the beginning of the line, then RE will use ^from.

#!python>>> print re.search(‘^From‘, ‘From Here to Eternity‘)<re.MatchObject instance at 80c1520>>>> print re.search(‘^From‘, ‘Reciting From Memory‘)None

$

Matches the end of a line, and the end of the line is defined as either the end of the string or any position following a newline character.

#!python>>> print re.search(‘}$‘, ‘{block}‘)<re.MatchObject instance at 80adfa8>>>> print re.search(‘}$‘, ‘{block} ‘)None>>> print re.search(‘}$‘, ‘{block}\n‘)<re.MatchObject instance at 80adfa8>

Match a "$", use $ or include it in a character class, such as [$].

\a

Matches only the first string. When not in MULTILINE mode, \a and ^ are actually the same. However, they are different in the MULTILINE mode; \a just matches the first string, and ^ can also match any position in the string after the line break.

\z

Matches only at the end of the string. Matches only the end of the string.

\b

The word boundary. This is a 0-wide qualifier (Zero-width assertions) that matches only the first and final words of a word. A word is defined as an alphanumeric sequence, so the ending is marked with a white or non-alphanumeric character.

The following example matches only the entire word "class", and does not match when it is contained in another word.

#!python>>> p = re.compile(r‘\bclass\b‘)>>> print p.search(‘no class at all‘)<re.MatchObject instance at 80c8f28>>>> print p.search(‘the declassified algorithm‘)None>>> print p.search(‘one subclass is‘)None

When using this special sequence you should remember that there are two subtleties here. The first is the worst conflict between a Python string and a regular expression. In the Python string, "\b" is a backslash character, and the ASCII value is 8. If you do not use the raw string, then Python will convert "\b" to a fallback, and your RE will not match it as you would like. The following example looks like the re in front of us, but there is a "r" missing in front of the re string.

#!python>>> p = re.compile(‘\bclass\b‘)>>> print p.search(‘no class at all‘)None>>> print p.search(‘\b‘ + ‘class‘ + ‘\b‘)<re.MatchObject instance at 80c3ee0>

The second in the character class, this qualifier (assertion) does not work, and \b represents a fallback character to be compatible with the Python string.

\b

Another 0-wide qualifier (zero-width assertions), which is exactly the same as \b, matches only when the current position is not at the word boundary.

More meta-characters

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.