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

What are the special elements in Python regular expression syntax

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

Share

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

This article mainly explains "what are the special elements of Python regular expression grammar". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the special elements of Python regular expression grammar"?

Regular expression pattern

Pattern strings use special syntax to represent a regular expression.

Letters and numbers represent themselves. The letters and numbers in a regular expression pattern match the same string.

Most letters and numbers have different meanings when preceded by a backslash.

Punctuation marks match themselves only when they are escaped, otherwise they represent special meanings.

The backslash itself needs to be escaped using a backslash.

Since regular expressions usually contain backslashes, you'd better use raw strings to represent them. Pattern elements (such as r'\ tbrush, equivalent to\ t) match the corresponding special characters.

The following table lists the special elements in the regular expression pattern syntax. If you provide optional flag parameters while using the pattern, the meaning of some pattern elements will change.

The pattern description ^ matches the beginning of the string $matches the end of the string. . Matches any character, except for newline characters, and when the re.DOTALL tag is specified, it can match any character including newline characters. [...] Used to represent a set of characters, listed separately: [amk] matches' axiomagery'or'k' [^.] Characters that are not in []: [^ abc] matches characters other than a _ c Re* matches 0 or more expressions. Re+ matches one or more expressions. Re? Matches 0 or 1 fragment defined by the previous regular expression, and re {n} matches n previous expressions in a non-greedy way. For example, "o {2}" does not match "o" in "Bob", but can match two o in "food". Re {n,} exactly matches the n preceding expressions. For example, "o {2,}" does not match "o" in "Bob", but does match all o in "foooood". "o {1,}" is equivalent to "o +". "o {0,}" is equivalent to "o *". Re {n, m} matches n to m fragments defined by the previous regular expression, greedily a | b matches an or b (re) matches the expression in parentheses, which also means that a group of (? imx) regular expressions contain three optional flags: I, m, or x. Only the areas in parentheses are affected. The (?-imx) regular expression turns off the I, m, or x optional flag. Only the areas in parentheses are affected. (?: re) is similar to (...), but does not mean a group (? imx: re) uses I, m, or x optional flags (?-imx: re) in parentheses without using I, m, or x optional flags (? #...) Note. (? = re) forward positive delimiter. If there is a regular expression to. Indicates that if the current position is successfully matched, it succeeds, otherwise it fails. But once the included expression has been tried, the matching engine does not improve at all; the rest of the pattern also tries to the right of the delimiter. ?! Re) forward negative delimiter. Contrary to the affirmative delimiter; successful when the contained expression does not match at the current position of the string. (? > re) matches the independent pattern, omitting backtracking. \ W matches numeric underscore\ W matches non-numeric underscore\ s matches any white space character, which is equivalent to [\ t\ n\ r\ f]. \ s matches any non-empty character\ d matches any number, which is equivalent to [0-9]. \ D match any non-numeric\ A match string begins\ Z match string ends, and if there is a line break, only matches to the end string before the line break. \ z match string end\ G match the position where the last match was completed. \ b matches a word boundary, that is, the position between the word and the space. For example,'er\ b 'can match' er','in 'never' but not 'er'' in 'verb'. \ B matches non-word boundaries.' Er\ B' can match 'er',' in 'verb' but not 'er'' in 'never'. \ n,\ t, etc. Matches a newline character. Match a tab, etc.\ 1.\ 9 matches the contents of the nth group. \ 10 matches the contents of the nth packet, if it is matched. Otherwise, it refers to the expression of the octal character code. Thank you for reading, these are the contents of "what are the special elements in Python regular expression grammar". After the study of this article, I believe you have a deeper understanding of what are the special elements in Python regular expression grammar, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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