Basics of Regular Expressions in Python, check the link below π π
Also with table of contents about symbols and their usage in Regex
Regular Expressions Regex basic in Python π π
Links for other methods like search, flags and pre-compiled patterns in regular expression scroll down
π π
Python makes regular expressions available through the re module.
Replacing in Regex Python
Replacements can be made on strings using re.sub .
Replacing strings
import re print( re.sub(r"t[0-9][0-9]", "foo", "my name t13 is t44 what t99 ever t44") ) # Output: 'my name foo is foo what foo ever foo'
OUTPUT: my name foo is foo what foo ever foo
Using group references
Replacements with a small number of groups can be made as follows:
import re print( re.sub(r"t([0-9])([0-9])", r"t\2\1", "t13 t19 t81 t25") ) # Output: 't31 t91 t18 t52'
OUTPUT: t31 t91 t18 t52
However, if you make a group ID like ’10’, this doesn’t work: \10 is read as ‘ID number 1 followed by 0’. So you have to be more specific and use the \g notation:
import re print( re.sub(r"t([0-9])([0-9])", r"t\g<2>\g<1>", "t13 t19 t81 t25") ) # Output: 't31 t91 t18 t52'
OUTPUT: t31 t91 t18 t52
Using a replacement function
import re items = ["zero", "one", "two"] print( re.sub(r"a\[([0-3])\]", lambda match: items[int(match.group(1))], "Items: a[0], a[1], something, a[2]") ) # Output: 'Items: zero, one, something, two'
OUTPUT: Items: zero, one, something, two
ILLUSTRATION USING PYTHON3 CLI
For the first part in using regular expressions and matching the string
you can visit this link β> π π
Matching the beginning of a string (Regex) Regular Expressions in python
The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.
For the second part in using regular expression and searching the string
visit β> Searching β Regular Expressions (Regex) in Python
Precompiled_pattern β> π π
Precompiled patterns β Regular Expression(Regex) in Python
Compiling a pattern allows it to be reused later on in a program.
However, note that Python caches recently-used expressions, so βprograms that use only a few regular expressions at a time neednβt worry about compiling regular expressionsβ.
Flags in Regex π π Flags in Regular Expressions ( Regex ) in Pythonβ¦.FTC
For some special cases we need to change the behavior of the Regular Expression, this is done using flags. Flags can be set in two ways, through the flags keyword or directly in the expression.
Morae Q!
- Program to get dictionary items, use update module and items module.
- Regular expression allowing certain set of characters.
- Compute the Sum of Cosine Series.
- Find the Sum of Sine Series.
- Remove the Vowels from the string.
- Find All Non-Overlapping strings/characters using regular expressions .
- Check whether the given string is Panagram or not.
- Replacing strings using Regular Expression Regex.
- Check whether the given number is Strong number or not.
- Find the numbers divisible by a input numbers within the given range.
- Change the behaviour of the Regular Expression using flags.
- Find the exponentiation of a number.
- Find all prime numbers in a range using Sieve of Eratosthenes.
- Convert decimal equivalent into binary, hexadecimal and octal.
- Compute the area of a farmers field in feet.
- Compute time from number of days/hours to seconds.
- Precompiled patterns using Regular Expressions .
- Compute the price of the cake on the nth day.
- Compute the number of possible non-increasing arrays.
- Find the type of spinach using the calorie value.