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 metacharacters of re module in Python

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

Share

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

This article mainly introduces the relevant knowledge of "how to use the metacharacters of re module in Python". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use the metacharacters of re module in Python" can help you solve the problem.

Metacharacters are special characters with special meaning in regular expressions, and Python is no exception. They are used to indicate the occurrence pattern of leading characters (characters before metacharacters) in the target object.

In a regular expression, a set of characters specified in square brackets ( [] ) constitutes a character class.

#metacharacter sequences match any single character in the class>>> s = 'foo123bar'# 3 arbitrary consecutive characters match>> re.search ('[0- 9][0 - 9]', s)>> re.search ('[0-9][0 - 9]', 'foo456 bar')>> re.search ('[0-9][0 -9]', '234 baz')>> re.search ('[0-9][0 - 9]','qux678')#Mismatch>> print(re.search ('[0 - 9][0 - 9]', '12foo 34'))None

Wildcard dot ( . Metacharacters match any character except newline characters.

>>> s = 'foo123bar'>>> re.search('1.3', s)>>> s = 'foo13bar'>>> print(re.search('1.3', s))None

Metacharacters supported by re module

The following list is a description of metacharacters. It is convenient to classify metacharacters for memory. If you don't understand this, skip ahead and look at the following example.

Character Description\Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape character. For example,'n' matches the character 'n'.'\ n 'matches a newline character. The sequence '\' matches 'and'('matches'('.^ Matches the start of the input string. ^also matches the position after '\n' or '\r' if the RegExp object's Multiline property is set.$ Matches the end of the input string. If the RegExp object's Multiline property is set,$also matches the position before '\n' or '\r'. * Matches the preceding subexpression zero or more times. For example, zo* matches "z" as well as "zoo."* Equivalent to {0,}.+ Matches the preceding subexpression one or more times. For example,'zo+' matches 'zo' and 'zoo,' but not 'z'.+ Equivalent to {1,}.? Matches the preceding subexpression zero or once. For example,"do(es)?" "Can match"do"or"does".? Equivalent to {0,1}. {n}n Is a nonnegative integer. Match determined n times. For example,'o{2}' doesn't match 'o' in 'Bob,' but it does match two o's in 'food'. {n,}n is a nonnegative integer. Match at least n times. For example,'o{2,}' doesn't match 'o' in 'Bob,' but it matches all o's in 'foooood.' o{1,}'is equivalent to' o+'.' o{0,}'is equivalent to' o*'. {n,m}m and n are nonnegative integers, where n

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