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 regular expressions in Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use regular expressions in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Basic use of regular expressions

Re.match (matching xxxxx string) attempts to match a pattern match from the beginning of the string. The re.match method returns a matching object, otherwise it returns None. You can use the group (num) or groups () match object function to get the matching expression, and groups () returns a tuple.

Use regular expressions to match a single character

. Match any 1 character (except\ n) re.S can match newline

Re.I means case insensitive.

[a-c1-9] matches the characters listed in []

\ d matching numbers, that is, 0-9

\ d matches non-numeric, that is, not numeric

\ s match white space, that is, space, tab key

\ s matching is not blank

\ w matches the character of the word, namely amurz, Amurz, 0-9. The default is re.U, that is, Unicode code, which can match Chinese characters. Plus re.A, you can specify ASCII code. [a-zA-Z0-9]

\ W matches non-word characters

Use regular expressions to match multiple characters

Match the previous character 0 times or unlimited times, that is, optional

Match the previous character once or indefinitely, that is, at least once

? Match the previous character once or 0 times, that is, either once or not

{m} matches the previous character m times

{mdirection n} matches the previous character from m to n

{m,} matches the previous character m to infinite times

Use of matching beginning and end boundaries

The beginning of the ^ match string [^ a] represents as long as it is not a

Match the end of the string

Use of matching grouping

| | matches any expression on the left or right |

(ab) Group the characters in parentheses as a group

\ num references the string matched by the grouping num\ 1\ 2

(? P) group with aliases

(? P=name) refers to the string to which the alias is matched by the name group

The use of re advanced functions such as search, findall, sub, etc.

Re.search scans the entire string and returns the first successful match. If the match succeeds, the re.search method returns a matching object, otherwise it returns None. You can use the group (num) or groups () match object function to get the matching expression.

Re.findall

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

Re.sub is used to replace matches in a string

Re.split cuts strings based on matches and returns a list

The difference between re.match and re.search

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

The characteristics and usage of greed and non-greed

Regular matching in Python is greedy by default (or non-greedy in a few languages), always trying to match as many characters as possible; non-greedy, on the contrary, always trying to match as few characters as possible. Add after "*", "?", "+", "{mrecoery n}"? To turn greed into non-greed

The function of adding r before a string

In Python, the string is preceded by r to indicate the original string.

Thank you for reading! This is the end of the article on "how to use regular expressions 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, you can 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