Get the App
SLTechnology News&Howtos  ›  Database  › 

Recognize regular expressions

Shulou Source: shulou.com Published: 2022-06-01 14:01:13 09月19日 Update

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 ()

Tags: Character string regular expression index search number dictionary method module pattern example syntax iteration Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei OPPO Reno vpn Docker