Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report