current position:Home>Necessary knowledge of Python: take you to learn regular expressions from zero

Necessary knowledge of Python: take you to learn regular expressions from zero

2022-01-30 02:46:16 Skin shrimp

Little knowledge , Great challenge ! This article is participating in “   A programmer must have a little knowledge   ” Creative activities

This article also participates in  「 Digging force Star Program 」  , Win a creative gift bag , Challenge creation incentive fund

Code Pipi shrimp A simple and interesting boy with sand sculpture , Like most of my friends, I like listening to music 、 game , Of course, in addition to this, there is an interest in writing ,emm..., It's a long time , Let's work hard together

If you think it's good , The ball is a concern


1、 Regular introduction

A regular expression is a special sequence of characters , It can help you easily check whether a string matches a certain pattern .

Python since 1.5 Version has been added ==re== modular , It provides Perl Style regular expression pattern , You can use ==import re== Import ==re== modular .


2、 Method

Common patterns of regular expressions  Insert picture description here Special characters  Insert picture description here

Regular expression modifiers - Optional logo  Insert picture description here

2.1、match()

re.match Try... From string == The starting position == Match a pattern , If it's not a successful start match ,match() Just go back to none.

Function syntax

re.match(pattern, string, flags=0)

Parameters describe
pattern Matching regular expressions
string String to match .
flags Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

 Insert picture description here  Insert picture description here

=='.' : Matches any character ( except \n)==  Insert picture description here  Insert picture description here

==/d: Match a number ==  Insert picture description here ==/D: Match a non number ==  Insert picture description here

==/s: Match blanks ==  Insert picture description here

==\d+: Match multiple numbers ==  Insert picture description here


2.2、search()

re.search == Scan the entire string == And return the first successful match .

Function syntax

re.search(pattern, string, flags=0)

Parameters describe
pattern Matching regular expressions
string String to match .
flags Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

 Insert picture description here

Once matched successfully, it will return , Will not continue to match

==re.match And re.search The difference between ==

re.match Match only the beginning of the string , If the string doesn't start with a regular expression , The match fails , The function returns None; and re.search Match the entire string , Until we find a match .


2.3、 Search and replace

re The module provides re.sub Used to replace matches in strings .

grammar

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

Parameters

  1. pattern : Pattern string in regular .
  2. repl : Replaced string , It can also be a function .
  3. string : The original string to be found and replaced .
  4. count : The maximum number of substitutions after pattern matching , Default 0 Means to replace all matches .

take text Medium 5 Replace with 777

 Insert picture description here

 Insert picture description here


2.4、compile()

compile Function to compile regular expressions , Generate a regular expression ( Pattern ) object , for match() and search() These two functions use .

Grammar format

re.compile(pattern[, flags])

Parameters

  1. pattern : A regular expression in the form of a string
  2. flags, Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

 Insert picture description here


2.5、findall()

** Find all the substrings that the regular expression matches in the string , And return a list , If no match is found , Then return to the empty list .

== Be careful ==: match and search It's a match findall Match all .**

Grammar format

findall(string[, pos[, endpos]])

Parameters

  1. string : String to match .
  2. pos : Optional parameters , Specifies the starting position of the string , The default is 0.
  3. endpos : Optional parameters , Specify the end of the string , The default is the length of the string .

 Insert picture description here


2.6、split()

split Method to split the string according to the matching substring and return the list

Grammar format

re.split(pattern, string[, maxsplit=0, flags=0])

Parameters describe
pattern Matching regular expressions
string String to match .
maxsplit Number of separations ,maxsplit=1 Separate once , The default is 0, Unlimited times .
flags Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

 Insert picture description here


2.7、 Regular expression modifiers - Optional logo

Regular expressions can contain optional flag modifiers to control the matching pattern .

 Insert picture description here  Insert picture description here

Select columns

offer one's own services , Recommend your own column , Welcome to collect and pay attention to

General column

Xiaobaixue Java

MybatisPlus special column

App Crawler column

PC End crawler column

Interview question column of big factory

The column of force deduction problem solving

Last

I am a Code Pipi shrimp , A lover of sharing knowledge Shrimp lovers , In the future, we will continue to update blog posts that are beneficial to you , We look forward to your attention !!!

It's not easy to create , If this blog post is helpful to you , I hope you can == One button, three links !==, Thank you for your support , See you next time ~~~


 One key, three links .png

copyright notice
author[Skin shrimp],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300246148440.html

Random recommended