Re --- Python Regular Expression Module

Source: Internet
Author: User

Re --- Python Regular Expression Module

Re is the most common Regular Expression module in Python. Common methods include compile, match, findall, finditer, search, split, and sub.

When some character strings are not easy to use, you can use the re module to conveniently perform operations such as search and replacement.

1, compile

Regular Expressions are compiled in advance to save time for subsequent reuse.

For example

>>> Import re >>> url = "http: // 10.128.39.48: 8058/net_command" # compile a regular expression object reg. # reg can be called by multiple methods, such as re. match (), re. findall (), re. sub (), etc. >>> reg = re. compile ('^ http :\/\/(. *?) :( \ D + ?) \/Net_command ') # perform regular expression matching on the url, and then use group () to obtain the matching result >>> result = reg. match (url) >>> result. group () 'HTTP: // 10.128.39.48: 8058/net_command '>>> result. group (0) 'HTTP: // 10.128.39.48: 8058/net_command '>>> result. group (1) '10. 128.39.48 '>>> result. group (2) '123'

2. re. match ('P', 'python') matches the regular expression at the beginning of the string. If the regular expression does not match the regular expression at the beginning, the match fails.

And re. search ('net _ command', url) scans the entire string until the first matching result is found and returned. the matching results can also be obtained through the group () method. 3, re. findall ('net _ command', url) searches for all matching results and returns the results list. and re. finditer () finds matching results and returns them as an iterator. 4, re. sub ('net _ command', 'COMMAND _ net', url) is used to replace matching results, which is equivalent to url. replace ('net _ command', 'COMMAND _ net '). the fourth parameter of the sub () method indicates the number of replicas. By default, 0 indicates all replicas. 5, re. split ('/', url), equivalent to url. split ('/'), the third parameter of the split () method indicates the number of splits. The default value 0 is all. 6. start () and end () indicate the start and end indexes of the matching results respectively. span () returns the tuples composed of the start and end indexes. 7. Regular Expression parameter, re. VERBOSE (or re. x) the regular expression can be structured and the form is easier to read. re. DOTALL (or re. s). match any character including linefeed. re. IGNORECASE (or re. i) makes the matching case insensitive. re. MULTILINE (or re. m) makes the multi-row match take effect, affecting the first and last matching of ^ and $. 8. Compared with the re module, we should try to use some operation methods of the character string, such as replace and translate, to replace the sub-string (replace re. sub (), index, and find are used for search operations (replace re. search () and re. match ()).

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.