Python-based regular expression

Source: Internet
Author: User

For many technical engineers who need to work with text, you must have a thorough understanding of Python regular expressions, not only to understand what a Python regular expression is, but also to know about Python regular expression characters.

In addition, a few characters are special, they do not match themselves, but rather than their literal value of some special things to match, these things may be character sets, repetition or location. Common meta-characters include:

. ^ $ * + ? { } [ ] \ | ( )

For these special characters, this article will be introduced in succession. But let's take a look at the meta-characters used to match characters. First, the period "." This meta-character is often used to match "any character": In general, it matches any character except for a newline character; in alternate mode (re). Dotall), it matches any character in the real sense, including the newline character

The next metacharacters we examine are "[" and "]". They are often paired to specify a character set to match, meaning that any element within the set satisfies our requirements. The characters within the collection can be listed individually, and if the characters are contiguous, you can also specify a range of characters by using the "-" number two given character.

For example, [ABC] will match any one of the characters in "a", "B" or "C", and of course you can use the interval [a-c] to represent the same character set, which means the method is equivalent. If you want to match all the vowels in a string, you can use the following code:

Import re def re_show (Pat, s):     

  

For a character set, it is important to note that metacharacters are "demoted" to ordinary characters in square brackets. For example, [A.] Matches the character "a" or "." Any one of the preceding words, said "." Typically used as a meta-character. But in the character set, its particularity will be stripped and restored to ordinary characters. The reader can modify the above code to experiment.

Sometimes you need to find characters that are not part of a character set. For example, if you want to find any character other than the number 6, then you need to use the antisense: It is a meta-character "^" as the first character of the collection, for example, [^5] will match any character except "6".

A backslash "\" is a very important meta-character. We know that in a python string, the backslash is also used as a special character (or escape character), which can be used to utilises different characters, and it can also use to cancel all metacharacters so that you can match them in the pattern. For example, if you need to match the character "\", you can use a backslash before it to cancel their special meaning: \ \.

\d matches any decimal number; it is equivalent to the character set [0-9]. \d matches any non-numeric character; it is equivalent to the character set [^0-9]. \s matches any whitespace character; it is equivalent to the character set [\t\n\r\f\v]. \s matches any non-whitespace character; it is equivalent to the character set [^ \t\n\r\f\v]. \w matches any alphanumeric underscore character, which is equivalent to the character set [a-za-z0-9_]. \w matches any non-alphanumeric underscore character, which is equivalent to the character set [^a-za-z0-9_].

Python-based regular expression

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.