In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what's so special about Python regular expressions". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what is special about Python regular expressions.
What is a Python regular expression
When writing programs or web pages that deal with strings, there is often a need to find strings that conform to some complex rules (or patterns). Regular expressions are the tools used to describe these rules (or patterns). In other words, a regular expression is the code that records text rules. Once you have found a text that meets the requirements, you can modify it accordingly.
Remember the wildcards used for file lookup on the Windows command line, namely * and?. When we look up all the PDF documents in a directory, just search for * .pdf. Here, * is interpreted as an arbitrary string. Like wildcards, regular expressions are also used for text matching, except that they describe your needs more accurately than wildcards, such as finding all phone numbers in a web page.
We know that telephone numbers are generally written in a fixed way: area code-phone number, that is, it begins with 0, followed by 2-3 digits, followed by a hyphen "-", and * is a string of 7 or 8 digits (for example, 010-12345678 or 0634-1234567).
Second, the simplest regular expression
The way to learn regular expressions is to start with specific examples and let readers experiment with them. Many simple examples are given below, and they are explained in detail. When we look for to in a string, you can use the regular expression to. This is almost the simplest regular expression, which can exactly match such a string
It consists of two characters, the first character is t and the second character is o. To demonstrate, we provide a function re_show (), which can be thought of as a wrapper for the re module, which matches a given string (that is, a string matches a regular expression).
It usually means that some or some or all of the string satisfies the conditions given by the expression) with a curly bracket. We will not introduce this function any further now. All you need to know is that the * argument of re_show () is a Python regular expression, the second argument is the string to match, and when you find a match, just enclose it in curly braces. Or the source code is as follows:
Import re def re_show (pat, s): print re.compile (pat, re.M) .sub ("{\ g}", s.rstrip ()),'\ ns =''Python runs on Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm Handhelds, and Nokia mobile phones. Python has also been ported to the Java and .NET virtual machines.''' " Re_show ("to", s)
Among them, the function call re_show ("to", s) is to find out whether the string s contains the string to, or whether the string s matches the regular expression to;. If so, add curly braces to the string to.
Third, the Python regular expression of matching words
We can see that the above regular expression is fine if you just want to find the string to in the text, but if you want to match the word to in the text, the above regular expression to is not enough. For example, change the definition of the string s in the above code to look like this:
S ='In company or association with respect to place or time; as, to live together in one house; to live together in the same age; they walked together to the town.'''
We find that the above text contains the string to not only in the word to, but also in words like together and town. If you use the above Python regular expression to to find the "word" to, you will make an error.
At this point, I believe you have a deeper understanding of "what is special about Python regular expressions". 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.