
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How does nested character class subtraction work in Python?
Nested Character Class Subtraction
Since we can use the full character class syntax within the subtracted character class, we can subtract a class from the class being subtracted. [0-9-[0-7-[0-3]]] first subtracts 0-3 from 0-7, yielding [0-9-[4-7]], or [0-38-9], which matches any character in the string 012389.
The class subtraction is always the last element in the character class. [0-9-[4-7]a-d] is not a valid regular expression. It should be rewritten as [0-9a-d-[4-7]]. The subtraction works on the whole class.
While we can use nested character class subtraction, we cannot subtract two classes sequentially. To subtract ASCII characters and Arabic characters from a class with all Unicode letters, combine the ASCII and Arabic characters into one class, and subtract that, as in [\p{L}-[\p{IsBasicLatin}\p{IsArabic}]].
- Related Articles
- How does class inheritance work in Python?
- Character class: subtraction - Java regular expressions
- How do nested functions work in Python?
- How does underscore "_" work in Python files?
- How does garbage collection work in Python?
- How does issubclass() function work in Python?
- How does isinstance() function work in Python?
- How does Overloading Operators work in Python?
- How does tuple comparison work in Python?
- How does ++ and -- operators work in Python?
- How does variable scope work in python?
- How does Python while loop work?
- How does a Python interpreter work?
- How does variable scopes work in Python Modules?
- How does [d+] regular expression work in Python?
