Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Python Retrieval and replacement

Shulou Source: shulou.com Published: 2022-06-01 01:33:27 09月22日 Update

In this article, the editor introduces in detail "how to achieve Python retrieval and replacement". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve Python retrieval and replacement" can help you solve your doubts.

Retrieve and replace

Python's re module provides re.sub to replace matches in strings.

Syntax:

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

Parameters:

Pattern: the pattern string in the regular.

Repl: the replacement string, which can also be a function.

String: the original string to be found and replaced.

Count: the maximum number of substitutions after a pattern match. The default of 0 means replacing all matches.

Flags: the matching pattern used at compilation time, in digital form.

The first three are required parameters and the last two are optional parameters.

Example

#! / usr/bin/python3

Import re

Phone = "2004-959-559 # this is a phone number" # Delete the comment

Num = re.sub (ritual, phone)

Print ("phone number:", num) # move unless the content of the number

Num = re.sub (r'\ Downs, "", phone)

Print ("telephone number:", num)

The execution result of the above example is as follows:

Telephone number: 2004-959559 telephone number: 2004959559 read here, this article "how to achieve Python retrieval and replacement" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.

Tags: Number phone number search character string content parameter article pattern instance number original proper maximum three two function form train of thought Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MariaDB Huawei Microsoft