In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "re module analysis of python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the re module analysis of python.
Sub
Parameter description:
Re.sub (pattern, repl, string, count=0, flags=0)
Patten: regular expression.
Repl: the string to replace.
String: the string to match.
Count: replacement parameter. The default value of 0 means there is no limit on the number of times. Parameters can be passed to specify the number of replacements.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Return value:
The return value is the new string after replacement.
Purpose:
The contents of the string are replaced by the repl parameter according to the regular expression.
Case study:
Print (re.sub ("123abc567def98ghi1") print (re.sub)) out:--abc--def--ghi--abc567def98ghisubn
Parameter description:
The parameters are exactly the same as sub.
Return value:
The return value is a tuple, element 0 is the new string after replacement, and element 1 is the number of successful replacements.
Purpose:
The contents of the string are replaced by the repl parameter according to the regular expression.
Case study:
Print (re.subn ('\ dabc567def98ghi`) str1, times = re.subn ('ABC',', '123abc567def98ghi') print (str1, times) out: ('-- abc--def--ghi', 3) 123abc567def98ghi 0finditer
Parameter description:
The parameters are exactly the same as findall.
Return value
Match starts from the beginning of the string, and there is no match until the end. If you return None; match to the result, you will return an iterator. The content of the iterator is a re.Match object (for more information on re.Match object, please see the return value section of re.search).
Purpose:
Using the way of iterator to find or extract the content according to the regular expression from the string, the advantage is to save memory, and the disadvantage is that the value is not intuitive.
Case study:
Result = re.finditer ('\ dflowers) out:12356798complie 123abc567def98') for i in result:print (i.group ())
Parameter description:
Re.compile (pattern, flags=0)
Patten: regular expression.
Flags: flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Return value
The regular expression class (class' re.Pattern') is returned.
Purpose:
Precompile a regular expression, and it is recommended to precompile a regular expression when it needs to be used multiple times, which can improve the running efficiency.
Case study:
Ret = re.compile ('\ dbath') res1 = ret.search ('123abc567def98') res2 = ret.findall (' 123abc567def98') print (res1) print (res1.group ()) print (res2) out:123 ['123,' 567, '98'] fullmatch
Parameter description:
The parameters are exactly the same as match.
Return value:
The return value is exactly the same as match. The difference is that match is matched at the beginning, and the re.Match object is returned if the match succeeds; fullmatch is an exact match, and re.Match is returned from beginning to end, and None is returned if the match fails.
Purpose:
Verify that the content entered by the user fully conforms to the regular expression.
Case study:
Result1 = re.fullmatch ("123abc567def98') print (result1) if result1:print (result1.group ()) result2 = re.fullmatch (" 12345678') print (result2) print (result2.group ()) out:None12345678escape
The escape function can be used to cancel escape characters, but it is not recommended to learn and use it. Please learn regular expressions well and forget about escape.
Re module flags
Pay attention to the functions of the re module, which almost all have flags parameters. The flags parameter can modify some of the way regular expressions work. Multiple flags can take effect simultaneously by using the bitwise operator |. Such as re.I | re.M. Flags can be written in two forms, either abbreviated or fully written.
I or IGNORECASE
Ignore letter case.
L or LOCALE
Affects\ w,\ W,\ b, and\ B, depending on the localization settings.
M or MULTILINE
After using this flag, when'^ 'and' $'match the beginning and end of a line, the position before and after the newline character will be increased.
S or DOTALL
Make "." Special characters exactly match any character, including newline characters (without this flag, "." Matches any character except a newline character.
X or VERBOSE
When the flag is specified, the space character in the regular expression is ignored unless the space character is in the character group or after the backslash. It also allows you to write comments with # in regular expressions.
Case study:
Result = re.findall ('[Amurz] +', '123abc567def98HIJ') print (result) result = re.findall (' [Amurz] +', '123abc567def98HIJJJI) print (result) out: [' abc', 'def'] [' abc', 'def',' HIJ'] so far, I believe you have a deeper understanding of "re module analysis of python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.