Master Python Regex Brackets: 8 Practical Examples Explained
This article explains Python’s regular expression bracket syntax, demonstrating eight practical examples—from simple character sets to negated classes and range patterns—while showing expected matches and non‑matches, helping readers master regex bracket usage for tasks like phone number validation.
This article introduces the special characters of Python regular expressions, focusing on the use of square brackets [] and their various meanings.
1. Simple character set : The pattern [abcd] matches any one of the characters a, b, c, or d at the beginning of the string. When applied to the string cpeng123, no match occurs; with dpeng123, the first character matches and the result is displayed.
2. Different first character : Changing the original string to acpeng123 makes the first character a, which satisfies the pattern [abcd], so the match succeeds and the output is acpeng123.
3. No matching character : With the string ecpeng123, none of the characters a‑d appear at the start, so the pattern [abcd] finds no match and nothing is output.
4. Phone number extraction : Using the range syntax [0-9], the pattern (1[34578][0-9]{9}) matches an 11‑digit phone number that starts with 1, followed by 3,4,5,7, or 8, and then nine digits. When the source string satisfies this rule, the phone number is output.
5. Different prefix : Changing the phone number to start with 160 makes the pattern fail, resulting in no output.
6. Negated character class : Adding ^ inside brackets creates a negation. The pattern (1[34578][^1]{9}) matches strings that start with 1, have a second digit 3‑5‑7‑8, and contain no 1 in the remaining nine positions. The example string without any 1 after the second character matches successfully.
7. Additional test : Changing the string to 180426825151 (adding a trailing 1) makes the negated pattern fail, so no output is produced.
8. Summary of bracket usages :
Any single character from the set, e.g., [abcd].
Character ranges, e.g., [0-9], [a-z], [A-Z].
Negation, using [^], e.g., [^1] matches any character except 1.
Literal characters inside brackets, such as [.] or [*], which match the dot or asterisk themselves.
Understanding these bracket patterns is essential for effective pattern matching in Python regular expressions.
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.
