Master Python Regex: Using $ and ? with Non‑Greedy Patterns
This tutorial explains Python regular expression special characters like $ and ?, demonstrates greedy versus non‑greedy matching with practical examples, and shows how to capture substrings correctly for web‑crawling tasks.
Previously we introduced basic Python regular expressions; now we dive deeper into special characters and non‑greedy matching.
1. The "$" character
The "$" symbol anchors the pattern to the end of a string. For example, the regex 3$ matches any string that ends with the character 3. An illustration is shown in the image above.
The pattern .*3$ therefore matches any sequence of characters that ends with 3, and it returns the original string as a match.
2. Matching a different ending
Changing the pattern to .*4$ looks for strings ending with 4. Since the sample input does not end with 4, no match is returned, as shown in the second image.
3. The "?" character
The "?" modifier switches the engine to a non‑greedy (lazy) matching mode. By default, regular expressions are greedy, meaning they try to consume as much of the input as possible.
4. Capturing with parentheses
Parentheses create capture groups. The pattern .*(p.*p).* means: any characters (greedy), then a p, followed by any characters (greedy), another p, and finally any characters. The intention is to extract the substring between the two p characters together with the p s.
However, because the surrounding .* parts are greedy, the engine matches the shortest possible segment, resulting in pp instead of the expected pccccccccccp.
5. Introducing non‑greedy before the first p
Changing the pattern to .*?(p.*p).* makes the first .* lazy. The match now starts from the left, moving toward the desired result, but the second p is still matched greedily, so the output contains an extra p.
6. Making both p parts non‑greedy
Using .*?(p.*?p).* applies the lazy qualifier to both sections. Now both p segments are matched from left to right, producing the expected substring.
7. Result of pure non‑greedy matching
When only the non‑greedy mode is used, the pattern returns pcccp.
8. Mixing greedy and non‑greedy
If greedy and non‑greedy parts coexist, the match can produce pcccpcccccccpppp.
Understanding and correctly applying non‑greedy patterns is crucial for extracting strings accurately in web‑crawling scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
