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, but Win/linux/mac have a simple running environment
Two application scenarios:
- If it is not in a folder, insert the folder name in the previous
- Add a prefix to all filename names
Just look 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 used to compile a regular expression and return an object
- P.finditer return all matching iterators
- P.sub passes the 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
Python uses regular expression text substitution