Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to add, delete and replace functions by using regular expressions

Shulou Source: shulou.com Published: 2022-06-02 12:26:41 09月24日 Update

This article shows you how to use regular expressions to add, delete, and replace functions, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

#-*-conding:utf-8-*-import retxt = 'the volume of this pool is about 34 cubic meters' role is the matching rule The format of re_out for replacement part is "\ 1 (replacement part)"''def Replace (role,re_out): out = re.compile (role) String_out = out.sub (re_out,txt) print (String_out) Replace (r' (the volume of this pool is about 34) (. *)', r'\ 1 square meter')''role is the matching rule Num is the element to be deleted (starting from 1)''def Delete (role,num): out = re.search (role,txt) .group (num) string = txt.split (out) String_out =' .join (string) print (String_out) Delete ('(the pool is about 34) (. *) (volume)', 2) def Add (role,re_out): out = re.compile (role) String_out = out.sub (re_out) Txt) print (String_out) Add (r'(the volume of this pool is about 34) (. {0})', r'\ 1')

The following picture is the result of the editor running these three functions. You can take a look at it. If you need it, you can refer to it. In addition, the editor needs someone to tell me how to implement this regular expression with automata and what his principle is. Welcome your comments.

The above content is how to use regular expressions to add, delete, and replace functions. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Volume pool regular expression function content volume skills knowledge rules parts concise concise three elements functions principles pictures that is articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Shulou Technology Shulou Tech Info MariaDB