In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "Python how to define regular expressions to achieve the shortest matching", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Python defines the regular expression to achieve the shortest match".
1. Demand
We are trying to match text patterns with regular expressions, but what is identified is the longest possible match. Instead, we want to modify it to the shortest possible match.
2. Solution
This problem usually occurs when the matching text is wrapped by a pair of beginning and ending delimiters (such as a quoted string). To illustrate this problem, take a look at the following example:
Import restr_pat=re.compile (r'\ "(. *)\") text1='mark say "love" 'text2='mark say "love", jingjing say "yes"' print (str_pat.findall (text1)) print (str_pat.findall (text2)
Results:
['love'] [' love ", jingjing say" yes']
In this example, the pattern r'"(. *)" tries to match the text contained in quotation marks. However, the * operator uses a greedy strategy in regular expressions, so the matching process is based on finding the longest possible match. That's why the match [love ", jingjing say" yes] appears above.
To solve this problem, just add after the * operator in the pattern? Modifiers are fine.
Example:
Import restr_pat=re.compile (r'\ "(. *?)\") text1='mark say "love" 'text2='mark say "love", jingjing say "yes"' print (str_pat.findall (text1)) print (str_pat.findall (text2)
Results:
['love']
['love',' yes']
This ensures that the matching process will not be greedy and will result in the shortest match.
At this point, I believe you have a deeper understanding of "Python how to define the regular expression to achieve the shortest match". 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.