Python re.sub

Source: Internet
Author: User

1.

  

re.sub? Signature:re.sub (Pattern, REPL, String, Count=0, flags=0) Docstring:return the string obtained by replacing the L Eftmostnon in string by Thereplacement repl.   or a callable; if  in it is processed.   is a callable, it ' s passed the match object and must returna replacement string to be used.

Parameter description: Pattern string, can be numerically named can also name name (\g<1>==\1) (?). P<name>----------------\g<name>)

Repl replaced string can also be a function string source string

Count substitution number of times

The value of flag is:

Re. I    make the match to case insensitive re. L    do localization recognition (locale-aware) to match re. M    multi-line match, affecting ^ and $re. S    makes. Matches all characters including line breaks RE.U    characters based on the Unicode character set. This flag affects \w, \w, \b, and \bre.x    this flag by giving you a more flexible format so that you can write regular expressions easier to understand

2. Example

  

def Replace_digit (m):     = u' 0 123456789 '    = int (m.group ())    return  = u'March 27, 1990 '= re.sub (u'\d' , Replace_digit, S, count=4)print#  March 27, 1990

' 2017-01-22 '  = re.sub ('(\d{4})-(\d{2})-(\d{2})', R'\2-\3-\1' , s) Print # 01-22-2017

  

Python re.sub

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.