Python Regular expression re (ii) escape

Source: Internet
Author: User
Tags alphanumeric characters

background :
In the process of using Python, you are certainly distressed about the use of escaped characters, because sometimes we need to use some special symbols such as "$ *". ^ "The original intention, sometimes need to be escaped after the function, and the escape character is cumbersome to use, error-prone, then save you are not re.escape."

Escape (Pattern)
Escape all non-alphanumeric characters in pattern.

Defined:
Re.escape (pattern) can be applied to all characters in a string that may be interpreted as a regular operator. You can use this function if the string is long and contains many special techniques, and you do not want to enter a large number of backslashes, or if the string is from the user (for example, by using the Raw_input function to get input), and is used as part of the regular expression.

To illustrate:

    • 1> re.escape(‘www.python.org‘)
    • 2‘www\\.python\\.org‘
    • 3
    • 4>re.findall(re.escape(‘w.py‘),"jw.pyji w.py.f")
    • 5[‘w.py‘, ‘w.py‘]

Here Re.escape (' w.py ') serves as the regular expression part of the function Re.findall function.

Python Regular expression re (ii) escape

Related Article

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.