In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "python regular expression re.search () how to use" related knowledge, Xiaobian through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope this "python regular expression re.search () how to use" article can help you solve the problem.
1 the function of re.search ():
Re.search matches the entire string and returns the first successful match. If the match fails, None is returned
You can see from the source code that there are three parameters in the re.search () method.
Pattern: matching rules
String: what to match
Flags flag bit is optional, that is, you don't have to write, you can write, for example, you can use flag bits if you want to ignore the case of characters.
The main contents of flags are as follows
Flags: optional, indicating matching pattern, such as ignoring case, multiline mode, etc. The specific parameters are:
Re.I ignores case
Re.L represents the special character set w, W, B, s, S depending on the current environment
Re.M multiline mode
Re.S is. And any character, including a newline character (. Does not include newline characters)
Re.U represents the special character set w, W, B, d, D, s, S depends on the Unicode character attribute database
Re.X ignores spaces and comments followed by # to increase readability
2 demo practice the use of re.search ()
2.1 search simple matching
Import re content = "abcabcabc" rex = re.search ("c", content) print (rex)
The print result is as follows
From the content, we can see that span (2,3) should be the corresponding subscript, so we can use span to get the matching subscript.
Match is the matching content, the content is c
2.2 get matching subscript
Import re content = "abcabcabc" rex = re.search ("c", content) print (rex.group ())
2.3 get the matching content, using group (the string of the matching entire expression)
Import re content = "abcabcabc" rex = re.search ("c", content) print (rex.group ())
Note that group and span cannot be used at the same time, otherwise an error will be reported
2.4 use flag bits to ignore matching case
Import re content = "abcabcabc" rex = re.search ("C", content, re.I) print (rex.group ())
The capital letter C is used here to match to c after ignoring upper and lower case.
2.5 use search to match the array in the string
Import re content = "abc123abc" rex = re.search ("d +", content) print (rex.group ())
2.6 use of search in conjunction with compile
Import re content = "abc123abc" rex_content = re.compile ("d +") rex = rex_content.search (content) print (rex.group ())
2.7 use of group
Import re content = "abc123def" rex_compile = re.compile ("([Amurz] *) ([0-9] *) ([Amurz] *)") rex = rex_compile.search (content) print (rex.group ()) print (rex.group (0)) # group () matches the whole print (rex.group (1)) # matches the content of the first parenthesis print (rex.group (2)) # match the content of the second parenthesis print (rex.group (3)) # match the content of the third parenthesis
There are not only numbers in group () parentheses, but can be customized as follows
Content = "zhangsanfeng108le" rex_compile = re.compile ((? P [amurz] *) (? P [0-9] *)) rex_content = rex_compile.search (content) print (rex_content.group ()) print (rex_content.group ("name")) # here the effect is equivalent to that of group (1) print (rex_content.group ("age")) # where the effect is equivalent to group (2) on "python regular expression" This is the end of the content of "how to use re.search ()". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.