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 π π
Python makes regular expressions available through the re module.
Find All Non-Overlapping Matches
Using re.findall(),
import re re.findall(r"[0-9]{2,3}", "some 1 text 12 is 945 here 4445588899") # Output: ['12', '945', '444', '558', '889']
[0-9] {2,3} it matches a number between 0-9 that is 2 to 3 digits. anything from 10 to 999 matches.
Note that the r before “[0-9]{2,3}” tells python to interpret the string as-is; as a “raw” string.
You could also use re.finditer() which works in the same way as re.findall() but returns an iterator with
SRE_Match objects instead of a list of strings:
import re results = re.finditer(r"([0-9]{2,3})", "some 1 text 12 is 945 here 4445588899") print(results) # Output: <callable-iterator object at 0x105245890> for result in results: print(result.group(0))
OUTPUT:
<callable-iterator object at 0x105245890>
12
945
444
558
889
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
Replacing Strings using Regex Python
Replacements can be made on strings using re.sub. π π π
Replacing Strings using Regular Expression Regex in Python..-FcukTheCode.com
Replacing strings and Using group references Replacements with a small number of groups can be made.
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.