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

Recognize regular expressions

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

The regular expression re in Python:

Pattern: matching patterns, following regular expression syntax

Method: matching method, search/match/split/findall/finditer/sub/subn

Introduction of re module

Re.search: search for a string and find the first string that matches

Re.match: starting with a string to match

Re.split: use regular expressions to separate strings

Re.findall: searches for matches from left to right based on regular expressions and returns a list of matching strings

Re.finditer: searches for matches from left to right based on regular expressions, returns an iterator and returns MatchObject

Re.sub: string substitution

Re.subn: like sub, returns the number of strings whose values are multiple substitutions

Re.group: returns the matching group: index 0 represents all matching strings, and index 1 starts to represent matching subgroups

Re.groupdict: returns a dictionary of named groups

Re.groups: returns matching subgroups, indexing all subgroups starting from 1

Example of operation explanation:

Re_demo (): txt=m=.search (, txt) m.groups () re_method (): s=.search (, s) .match (, s) .match (, s) re_method_2 (): s=.split (, s) re_method_3 (): s1=s2=.findall (, S1). Findall (, S2) re_method_4 (): S2 = i=.finditer ( S2) mi: m.group () re_method_5 (): S2 = .sub (, S2) .subn (, S2) re_method_6 (): data=m=.match (, data) m.group (,) m.groups () _ name__==: re_method_6 ()

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

Database

Wechat

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

12
Report