Python-Custom pattern string template for specific explanations

Source: Internet
Author: User

Custom pattern string Templates (template) for specific explanations


This address: http://blog.csdn.net/caroline_wendy/article/details/28625179


string. Template pattern is a regular form that enables you to define a new regular form by overriding the Pattern property .

For example: Use the new delimiter "{{" and {{var}} as the variable syntax.


Code:

#-*-Coding:utf-8-*-' Created on 2014.6.5@author:administrator@edition:python 3.3.0, Eclipse pydev ' import stringt = String. Template (' $var ') print (T.pattern.pattern) class MyTemplate (string. Template):    delimiter = ' {'    pattern = R ' '    \{\{(?

: (? p<escaped>\{\{) | # Escape sequence of delimiters (?

p<named>[_a-z][_a-z0-9]*) \}\} | # delimiter and a Python identifier {(? p<braced>[_a-z][_a-z0-9]*)}\}\} | # delimiter and a braced identifier (? p<invalid>) # other ill-formed delimiter Exprs ) ' t2 = MyTemplate (' ' {{{{{{{{ }}} ') Print (' MATCHES: ', T2.pattern.findall (t2.template)) print (' substituted: ', T2.safe_substitute (var= ' replacement '))


Output:

    \$(?:      (? p<escaped>\$) |   # Escape sequence of delimiters      (? p<named>[_a-z][_a-z0-9]*)      |   # delimiter and a Python identifier      {(? p<braced>[_a-z][_a-z0-9]*)}   |   # delimiter and a braced identifier      (? p<invalid>)              # other ill-formed delimiter Exprs    )    MATCHES:  [(' {' {', ' ', ' ', ' '), (', ' var ', ', ")]substituted:  {{replacement






Python-Custom pattern string template for specific explanations

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.