Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses the re.sub function

Shulou Source: shulou.com Published: 2022-06-01 02:32:27 09月21日 Update

Editor to share with you how python uses the re.sub function, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Re.sub

Re.sub is used to replace matches in a string. The following example replaces the space in the string''with' -':

Import retext = "PythonTab.com is a good Python website" print re.sub (r'\ s subscription,'-', text)

The function prototype of re.sub is: re.sub (pattern, repl, string, count)

Where the second function is the replaced string; in this case,'-'

The fourth parameter refers to the number of substitutions. The default is 0, which means that each match is replaced.

Re.sub also allows the use of functions to perform complex processing of the replacement of matches. For example: re.sub (r'\ s', lambda m:'['+ m.group (0) +']', text, 0); replace the space in the string''with' []'.

These are all the contents of the article "how python uses re.sub functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Functions characters strings articles content spaces complexity less number examples prototypes parameters most more knowledge industry information channels channels references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Microsoft Shulou Information Apple