Python string substitution re.sub ()

Source: Internet
Author: User

re.sub(pattern, repl, string, count=0, flags=0)

Pattern can be a string or a regular that matches the character to be replaced, and if not, the string is not modified. \1 represents the first group

REPL is the value that will be replaced, REPL can be a string, or it can be a method. If it is a string, the backslash is processed as an escape character, such as \ n is replaced with a newline, and so on. Repl if it is a function, each matching field string executes the replacement function.

\g<1> represents the first grouping in the preceding pattern, which can be abbreviated to \1,\g<0> representing all strings that match the preceding pattern.

count is the maximum number of times that pattern is replaced, and by default 0 replaces all. Sometimes you may want to replace only one part, you can use the Count

Example 1:

A = Re.sub (R ' Hello ', ' I love the ', ' Hello World ') print (a)
' I love the World ' #hello in world Hello is replaced by I love the

Example 2:

>>> a = Re.sub (R ' (\d+) ', ' hello ', ' My Numer is "and door num is") >>> a ' My numer is Hello and door Num is hello ' #数字400 and 200 are replaced by Hello

Example 3:

A = Re.sub (R ' Hello (\w+), Nihao \1 ', R ' Emma ', ' Hello Sherry, Nihao Sherry ') >>> a ' Emma '  #\1 represents the first grouped value as sherry, Because there are two sherry, so using \1 can refer to the second one, so that the entire string is replaced by Emma

Example 4:

>>> a = Re.sub (' (\d{4})-(\d{2})-(\d{2}) ', R ' \2-\3-\1 ', ' 2018-06-07 ') >>> a ' 06-07-2018 ' >>> a = Re.sub (' (\d{4})-(\d{2})-(\d{2}) ', R ' \g<2>-\g<3>-\g<1> ', ' 2018-06-07 ') >>> a ' 06-07-2018 '  #\2 and \g<2> refer to the second group in front

Example 5:

Import redef replace_num (str): numdict = {' 0 ': ' 0 ', ' 1 ': ' One ', ' 2 ': ' Two ', ' 3 ': ' Three ', ' 4 ': ' Four ', ' 5 ': ' Five ', ' 6 ': ' Six ', ' 7 ': ' Seven ', ' 8 ': ' Eight ', ' 9 ': ' Nine '}print (Str.group ()) return Numdict[str.group ()]my_str = ' June 7, 2018 ' A = Re.sub (R ' (\d) ', Replace_num, my_str) print (a)  #每次匹配一个数字, execute the function to get the replaced value

re.subn(patternreplstringcount=0flags=0)

As with the sub () function, only a tuple is returned, the number of substituted strings and replacements

 

 

Python string substitution re.sub ()

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.