In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the regular expressions commonly used in Python3". In the daily operation, I believe many people have doubts about the regular expressions commonly used in Python3. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what are the common regular expressions in Python3?" Next, please follow the editor to study!
Regular expression quick look-up table
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. Serial "\" matches "\" and "(" matches "(". ^ matches the starting position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r". $matches the end of the input string. If the Multiline property of the RegExp object is set, $also matches the position before "\ n" or "\ r". * matches the previous subexpression zero or more times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}. + matches the previous subexpression one or more times. For example, "zo+" matches "zo" and "zoo", but not "z". + is equivalent to {1,}. ? Matches the previous subexpression zero or once. For example, "do (es)?" Can match "do" in "does" or "does". ? It is equivalent to {0jue 1}. {n} n is a non-negative integer. Match the determined n times. For example, "o {2}" does not match "o" in "Bob", but can match two o in "food". {n,} n is a non-negative integer. Match at least n times. 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 *". {n ·m} m} m and n are nonnegative integers, where nx | y matches x or y. For example, "z | food" can match "z" or "food". "(z | f) ood" matches "zood" or "food". [xyz] character collection. Matches any of the characters contained. For example, "[abc]" can match "a" in "plain". [^ xyz] negative character collection. Matches any characters that are not included. For example, "[^ abc]" can match "p" in "plain". [amurz] character range. Matches any character within the specified range. For example, "[a murz]" can match any lowercase character in the range of "a" to "z". [^ amurz] negative character range. Matches any character that is not within the specified range. For example, "[^ amurz]" can match any character that is not in the range of "a" to "z". \ 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" matches "er" in "verb", but not "er" in "never". \ cx matches the control characters indicated by x. For example,\ cM matches a Control-M or carriage return. The value of x must be one of Amurz or aMuz. Otherwise, c is treated as a literal "c" character. \ d matches a numeric character. Equivalent to [0-9]. \ D matches a non-numeric character. Equivalent to [^ 0-9]. \ f matches a feed character. Equivalent to\ x0c and\ cL. \ nmatches a newline character. Equivalent to\ x0a and\ cJ. \ r matches a carriage return. Equivalent to\ x0d and\ cM. \ s matches any white space characters, including spaces, tabs, page breaks, and so on. Equivalent to [\ f\ n\ r\ v]. \ s matches any non-white space character. Equivalent to [^\ f\ n\ r\ t\ v]. \ t matches a tab. Equivalent to\ x09 and\ cI. \ v matches a vertical tab. Equivalent to\ x0b and\ cK. \ w matches any word characters that include underscores. Equivalent to "[A-Za-z0-9]". \ W matches any non-word characters. Equivalent to "[^ A-Za-z0-9]". \ xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be a determined two-digit length. For example, "\ x41" matches "A". "\ x041" is equivalent to "\ x041". ASCII encoding can be used in regular expressions. .\ num matches num, where num is a positive integer. A reference to the obtained match. For example, "(.)\ 1" matches two consecutive identical characters. \ nidentifies an octal escape value or a backward reference. If there are at least n previous acquired subexpressions, n is a backward reference. Otherwise, if n is an octal number (0-7), n is an octal escape value. \ nm identifies an octal escape value or a backward reference. If there are at least nm acquired subexpressions before\ nm, nm is a backward reference. If there are at least n fetches before\ nm, n is a backward reference followed by the text m. If none of the previous conditions are met, if both n and m are octal numbers (0-7),\ nm will match the octal escape value nm. \ nml if n is an octal number (0-3) and m and l are both octal numbers (0-7), the octal escape value nml is matched. \ un matches n, where n is a Unicode character represented by four hexadecimal digits. For example,\ u00A9 matches the copyright symbol (©).
Common functions:
1.re.match start position match, return (even if the regular expression does not start with ^ declaration match) input= "getting started" # P group name reg=re.compile (r'(? P getting started)) res=reg.match (input) if res: print (res.groupdict ()) print (res.groups ()) print (res.groups ()) {'name':' getting started'} ('getting started',) ('getting started' ) 2.re.search searches the entire string # scans the entire string to find the first location that matches the style And returns a corresponding matching object. If there is no match, return a None input=''' getting started tutorial''reg=re.compile (r' getting started (?: mini site | tutorial)', re.S) res=reg.search (input) if res: print (res.group ()) getting started 3.re.findall finds all matches in the string And return a listinput=''' getting started tutorial'# re.M multiline pattern matching reg=re.compile (r'^ getting started (?: tutorial) $', re.M) res=reg.findall (input) if res: print (res) ['getting started', 'getting started'] 4.re.split string Segmentation # split (pattern, string, maxsplit=0) Flags=0) # maxsplit maximum number of splits input=''' getting started with dd 'reg=re.compile (r' [a-z0-9] +') res=reg.split (input) print (res) reg=re.compile (r'[a-z0-9] +') res=reg.split (input,1) print (res) ['\ ngetting started\ ngetting started,\ ngetting started\ n`] [\ n getting started\ n' 5. Re.sub string replacement # Syntax sub (pattern, repl, string, count=0, flags=0) # repl replaced with the string # count specified number of substitutions input=''' getting started 456dd 'reg=re.compile ([a-z0-9] {1,})', re.M) res=reg.sub ('11') Input) print (res) # replace reg=re.compile once (r'([a-z0-9] {1,})', re.M) res=reg.sub ('11 minutes minute input1) print (res) entry 11 Mini Station 1111
Getting started with 11 stations and 456 dd
6. Re.subn# syntax subn (pattern, repl, string, count=0, flags=0) # repl is replaced with a string # count specifies the number of replacements # the number of times returned in the result includes substitutions input=''' getting started 456dd 'reg=re.compile (r' ([a-z0-9] {1,})', re.M) res=reg.subn ('11 getting started) print (res) ('\ ngetting started\ N11\ n11\ n11\ ngetting started\ n' 3) so far The study on "what regular expressions are commonly used in Python3" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.