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

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to analyze Python regular expressions. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Today, when I wrote a crawler, I accidentally thought of learning regular expressions. I read an article that was very good. I did a search and found it.

Re module

Re.search

The form match = re.search (pat, str) is often used. Because there may not be a match, re.search () is usually followed by if statement.

Re.match

Re.match is similar to re.search, except that re.match matches from the beginning of the string.

The meaning of commonly used regular characters

A, X, 9, and other characters match themselves, metacharacters do not match themselves, because they have special meanings, such as. ^ $* +? {} []\ | ()

. English full stop, matches any character, does not contain'\ n'

\ w matches the 'word' character, [a-zA-Z0-9]

\ W matches non 'word' characters

\ b match the boundary between 'word'' and 'non-word'

\ s matches a single whitespace character, space, newline, return, tab, form [\ n\ r\ t\ f]

\ s matches non-whitespace characters

\ t,\ n,\ r match tab, newline, return

\ d matching numbers [0-9]

^ matches the beginning of the string

Match the end of the string

Repetition

'+' one or more times,'* 'zero or more times,'?' Zero or once

Square brackets []

[] similar to or

Square brackets can be used to indicate a set of chars, so [abc] matches'a 'or 'b'or 'cages.

Group Extraction parentheses ()

Sometimes we need to extract part of the matching characters, such as the mailbox just now, where we may need username and hostname. At this time, we can wrap username and hostname with (), just like r' ([\ w. -] +) @ ([\ w. -] +)'. If the match is successful, then the pattern will not change, but we can use match.group (1) and match.group (2) to keep the username and hostname,match.group () results unchanged.

Findall and groups

() is combined with findall () and returns a list of tuples if one or more group is included.

It's the same after adding ^ to re.search.

Re.sub

Re.sub (pat, replacement, str) looks in str for a string that matches pattern, and then replaces it with replacement. Replacement can contain\ 1 or\ 2 to replace the corresponding group, and then implement a local replacement.

After reading the above, do you have any further understanding of how to analyze Python regular expressions? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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