Tools, FAQ, Tutorials:
re.sub() - Substitute Matches with String
How to substitutes matches of a regular expression in a target string with another string using re.sub()?
✍: FYIcenter.com
The re.sub() function allows you to substitute (or replace)
matches of a given regular expression in
match the beginning part of a target string
with of a given regular expression.
import re f = re.sub(pattern, repl, string, count=0, flags=0) Where: pattern is the regular expression repl is the replacement string string is the target string count=0 means replacing all matches f is the final string after replacements are done
Here is Python example that shows you how to use the re.findall() function:
>>> import re >>> s = "Hello perl.org, php.net, and python.org!" >>> p = "(\\w+)\\.(\\w+)" >>> print(p) (\w+)\.(\w+) >>> r = "domain" >>> f = re.sub(p,r,s) >>> print(f) Hello domain, domain, and domain!
⇒ Use Match Group \g<n> in Replacement
⇐ re.match() - Match from String Beginning
2018-10-19, 1488👍, 0💬
Popular Posts:
Can Multiple Paragraphs Be Included in a List Item? Yes. You can include multiple paragraphs in a si...
What is the "__init__()" class method? The "__init__()" class method is a special method that will b...
How to include additional claims in Azure AD v2.0 id_tokens? If you want to include additional claim...
How to add images to my EPUB books Images can be added into book content using the XHTML "img" eleme...
How to Build my "sleep" Docker image from the Alpine image? I want the container to sleep for 10 hou...