Example of using regular expressions to implement text substitution in Python

Source: Internet
Author: User
This article mainly introduces the method that Python uses regular expression to implement text substitution, analyzes the specific operation steps of Python using regular expression to realize text substitution with the example form, and the related usage precautions, the friend who need can refer to the following

Three-way client programming in a sense is the material organization, so, the picture material organization often need batch processing, Python must be the best choice, regardless of Win/linux/mac has a simple running environment

Two application Scenarios :

① if it is not in a folder, insert the folder name in front

② all filename names plus a prefix

Look directly at the code:


# Encoding:utf-8import re# compiles the regular expression into the pattern object P = Re.compile (R ' (? P<folder> (\w+/) *) (? p<filename>\w+\.png) ' # matches the text using pattern, gets the match result, cannot match when will return None#match = Pattern.match (' <key>xxx/ Duobaojiemian_l/yangpizi.png</key> ') The_str = "" "<key>xxxx/duobaojiemian2222_l/duobaojiemian_l/ yangpizi.png</key> <key>yangpizi2.png</key> <key>yangpizi3.png</key> "" "for M in P.finditer (THE_STR): # Use Match to get Group Info print m.groupdict () print '-------------------------------' #f = Lambda m:m.group () . Find (' xxxx/') = =-1 and ' xxxx/' +m.group () or M.group () def f (m): s = M.group () return S.find (' xxxx/') = =-1 and ' xxxx/' +s or sdef F2 (m2): D = m2.groupdict () return d[' folder ']+ ' the_ ' +d[' filename ']print p.sub (F2, THE_STR)

There are several questions about regular expressions that need to be accounted for.

①. Python Regular Expressions Use this syntax if the capture requires grouping (? p< naming > matching regular expressions)

②. Re.compile to compile a regular expression and return an object

③. P.finditer returns all matching iterators

④. P.sub passes a match to the callback function and replaces the text with the return value

⑤. M.groupdict, you can use the group name to take the corresponding value

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.