Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈

If you feel well written, ball ball a concern oh 😉


1. Introduction to re

A regular expression is a special sequence of characters that helps you easily check if a string matches a pattern.

Python has added the ==re== module since version 1.5, which provides perl-style regular expression patterns that can be imported using the ==import re== module.


2, methods,

Common patterns of regular expressions Special characters

Regular expression modifier – Optional flag

2.1, the match ()

Re.match attempts to match a pattern from the string’s == start position ==, and match() returns None if the match is not successful.

The function of grammar

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

parameter describe
pattern Matching regular expression
string String to match.
flags Flag bit used to control the matching of regular expressions, such as case – sensitive, multi-line matching, and so on.

==’.’ : matches any character except \n ==

==/d: Matches a number == ==/D: Matches a non-number ==

==/s: matches blank ==

==\d+ : Matches multiple numbers ==


2.2, the search ()

Re.search == scans the entire string == and returns the first successful match.

The function of grammar

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

parameter describe
pattern Matching regular expression
string String to match.
flags Flag bit used to control the matching of regular expressions, such as case – sensitive, multi-line matching, and so on.

A successful match is returned and no further matches are made

== the difference between re.match and re.search ==

Re.match matches only the beginning of the string. If the beginning of the string does not match the regular expression, the match fails and None is returned. Re.search matches the entire string until a match is found.


2.3. Search and Replace

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

grammar

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

Parameters:

  1. Pattern: Pattern string in the re.
  2. Repl: Replacement string, also available as a function.
  3. String: The original string to be searched and replaced.
  4. Count: indicates the maximum number of times to replace the matching mode. The default value 0 indicates that all matches are replaced.

Replace the 5 in text with 777


2.4, the compile ()

The compile function is used to compile regular expressions, producing a regular expression (Pattern) object used by the match() and search() functions.

Syntax format

re.compile(pattern[, flags])

parameter

  1. Pattern: A regular expression in string form
  2. Flags, which controls how regular expressions are matched, such as case sensitive, multi-line matching, and so on.


2.5, the.findall ()

** Finds all substrings matched by the regular expression in the string and returns a list, or an empty list if no matches are found.

== Note that == : match and search match once. Findall matches all. **

Syntax format

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

parameter

  1. String: indicates the string to be matched.
  2. Pos: Optional argument that specifies the start position of the string. Default is 0.
  3. Endpos: Optional argument that specifies the end position of the string. Default is the length of the string.


2.6, the split ()

The split method splits the string according to the substrings that can be matched and returns the list

Syntax format

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

parameter describe
pattern Matching regular expression
string String to match.
maxsplit Maxsplit =1 split once, default is 0, no limit on the number of times.
flags Flag bit used to control the matching of regular expressions, such as case – sensitive, multi-line matching, and so on.


2.7. Regular expression modifiers – Optional flags

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

🔥 Featured column

Self-introduction, to everyone recommend their column 😁, welcome small partners to collect attention 😊

🔥 total bar

The small white to learn Java

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column

Force buckle problem solving column

💖 finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time