The function prototype of re.sub is: re.sub(pattern, repl, string, count)
The second function is the replaced string; in this case it is '-'
The fourth parameter refers to the number of replacements. Defaults to 0, meaning every match is replaced.
re.sub also allows sophisticated handling of replacement of matches using functions. For example: re.sub(r'\s', lambda m: '[' m.group(0) ']', text, 0); Replace the space ' ' in the string with '[ ]'.
You can use re.split to split a string, such as: re.split(r'\s ', text); split the string into a word list by spaces.
re.findall can get all matching strings in the string. For example: re.findall(r'\w*oo\w*', text); Get all words containing 'oo' in the string.
Regular expressions can be compiled into a regular expression object. Frequently used regular expressions can be compiled into regular expression objects, which can improve certain efficiency.
All resources on this website are contributed and published by netizens, or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this website are for learning and reference only. Please do not use them for commercial purposes, otherwise you will be responsible for all consequences incurred! If there is any infringement, please contact us to delete and remove it. Contact information: admin@php.cn