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 use re Module in Python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the re module in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Introduction to Python--re Modul

Definition: re modules are called regular expressions

Function: create a "regular expression" to verify and find text that conforms to the rules, and is widely used in various search engines, account password verification, etc.

The predefined character ""\ d matches all decimal digits 0-9\ D matches all non-digits, including underscore\ s matches all white space characters (spaces, TAB, etc.)\ S matches all non-white space characters Contains underscore\ w matches all letters, Chinese characters, numbers amurz Amurz 0-9\ W matches all non-letters, Chinese characters, numbers, including underscore "" special characters

1. $: matches the end of a line (must be placed at the end of the regular expression)

2. ^: matches the beginning of a line (must be placed in front of the regular expression)

3. *: the preceding character can appear 0 or more times (0 ~ infinite)

4. +: the preceding character can appear one or more times (1 ~ infinite)

5 、? Change "greedy mode" to "reluctant mode", and the preceding characters can appear 0 or 1 times

6.: matches any single character except the newline character "\ n"

7. |: match both items

8. []: represents a collection, and there are three cases

[abc]: can match a single character in it

[a-z0-9]: can match a specified range of characters, but can be reversed (add ^ at the beginning)

[2-9] [1-3]: can do combination matching

9. {}: used to mark the frequency of the preceding characters, as follows:

{njudicial m}: indicates that the preceding characters appear at least n times and m times at most

{n,}: indicates that the preceding characters appear at least n times and are unlimited at most

{, m}: indicates that the preceding characters appear at most n times, at least unrestricted

{n}: the preceding character must appear n times

Backslash problem

If there is a backslash in the string, you need to escape the backslash:

Str = "\\ 123 223" #\ 123 223str = r "\ 123 223" #\ 123 223

In a regular expression, we need to match a backslash with multiple backslashes:

Find = re.search ('\ wrought, str) find = re.search (r'\ wrought, str) matching method

1. Match: match at the beginning of the target text

Find = re.math ('hello', str1) # returns the matching object hello if the match succeeds, but returns None if the match is not successful

2. Search: match in the entire target text

3. Findall: scan the entire target text and return a list of all the substrings that match the rule. If there is no match, an empty list is returned.

4. Finditer: scan the entire target text and return an iterator composed of all substrings that match the rule

5. Fullmatch: the target text is required to match the rule exactly, otherwise None is returned.

6. Sub: replace the substring that matches the rule with other text

Str1 = re.sub ('\ aaa', str, count=0) # count defaults to 0, which means replace all

7. Split: cut from the substrings that match the rule, and return the list of substrings formed after cutting

8. The method of matching objects (using for matching objects):

(): grouping characters, which can quickly obtain the data in the grouping for the matched content.

Group: used to view the content to which the specified packet is matched

Str ='

This is a text.

Find = re.search ('(\ w +)', str) print (find.group ()) # defaults to 0, indicating all the matched text. Pass in 1 and output the first packet #

Groups: returns a tuple with all the matching content in the group (the above case output ('# #', 'text'))

Groupdict: returns a dictionary containing the key-value pairs of the group, which needs to be named.

Find = re.search ('(? P\ w +)', str)

Start: returns the starting index of the matched content in the text end: returns the end index of the matched content in the text span: returns the tuple consisting of the starting index and the ending index

This is the end of the article on "how to use the re module in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report