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/03 Report--
This article introduces the knowledge about "how to use Python common regular functions". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Python's regular expressions have two main ways to accomplish pattern matching: "search" and "match"
re.match
re.match tries to match all or part of a pattern from the beginning of the string, e.g.: The following example matches the first word.
import retext = "PythonTab.com is a good Python website"m = re.match(r"(\w+)\s", text)if m:print m.group(0), '\n', m.group(1)else:print 'not match'
re.match(pattern, string, flags)
The first parameter is a regular expression, here "(\w+)\s", if the match is successful, it returns a Match, otherwise it returns a None;
The second parameter represents the string to match;
The third parameter is the flag bit, which controls how regular expressions match, such as case sensitivity, multiline matching, and so on.
Note: it can match the premise is: the beginning must meet the matching conditions
re.search
The re.search function looks for pattern matches within the string, only until the first match is found and returns, if no match is found in the string, returns None.
import retext = "PythonTab.com is a good Python website"m = re.search(r'\Pyt(on)n\s', text)if m:print m.group(0), m.group(1)else:print 'not search'
The prototype of re.search is: re.search(pattern, string, flags)
Each parameter has the same meaning as re.match.
The difference between re.match and re.search: re.match matches only the beginning of the string, if the string does not match the regular expression at the beginning, the match fails, and the function returns None; while re.search matches the entire string until a match is found.
re.sub
re.sub is used to replace matches in strings. Here's an example of replacing a space ' ' with a '-' in a string:
import retext = "PythonTab.com is a good Python website"print re.sub(r'\s+', '-', text)
re.sub(pattern, repl, string, count)
where the second function is the substituted string; in this case '-'
The fourth parameter refers to the number of substitutions. The default is 0, meaning every match is replaced.
Re.sub also allows complex handling of matching item substitutions using functions. For example: re.sub(r'\s', lambda m: '[' + m.group (0)+']', text, 0); replace spaces 'with'[ ]'in the string.
re.split
You can use re.split to split a string, e.g. re.split(r'\s+', text); to split a string into a list of words by spaces.
re.findall
re.findall gets all matching strings in a string. For example: re.findall(r'\w*oo\w*', text); Get all words in the string that contain 'oo'.
re.compile
You can compile a regular expression into a regular expression object. Regular expressions that are frequently used can be compiled into regular expression objects, which can improve certain efficiency. Here is an example of a regular expression object:
import retext = "PythonTab is a good Python website"regex = re.compile(r'\w*on\w*')print regex.findall(text) #Find all words containing 'on' print regex.sub(lambda m:'[' + m.group (0)+']', text) #Enclose words containing 'on' in a string with []. "How to use Python common regular functions" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.