Python makes regular expressions available through the re module.
Regular expressions are combinations of characters that are interpreted as rules for matching sub-strings.
For instance, the expression ‘amount\D+\d+’ will match any string composed by the word amount plus an integral number, separated by one or more non-digits, such as: amount=100 , amount is 3 , amount is equal to: 33 , etc.
The first argument of re.match() is the regular expression, the second is the string to match:
import re pattern = r"123" string = "123zzb" print(re.match(pattern, string))
OUTPUT:
<_sre.SRE_Match object; span=(0, 3), match=’123′>
match = re.match(pattern, string) print(match.group())
OUTPUT:
‘123’
ILLUSTRATION:
![](http://www.fcukthecode.com/wp-content/uploads/2022/01/Screenshot-from-2022-01-29-18-46-13.png)
You may notice that the pattern variable is a string prefixed with r , which indicates that the string is a raw string literal.
A raw string literal has a slightly different syntax than a string literal, namely a backslash \ in a raw string literal means “just a backslash” and there’s no need for doubling up backlashes to escape “escape sequences” such as newlines ( \n ), tabs ( \t ), backspaces ( \ ), form-feeds ( \r ), and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence.
Hence, r”\n” is a string of 2 characters: \ and n . Regex patterns also use backslashes, e.g. \d refers to any digit character. We can avoid having to double escape our strings ( “\d” ) by using raw strings ( r”\d” ).
For instance:
string = "\\t123zzb" # here the backslash is escaped, so there's no tab, just '\' and 't' pattern = "\\t123" # this will match \t (escaping the backslash) followed by 123 re.match(pattern, string).group() # no match, throws error re.match(pattern, "\t123zzb").group() # matches '\t123' pattern = r"\\t123" re.match(pattern, string).group() # matches '\\t123'
ILLUSTRATION (with output)
![](http://www.fcukthecode.com/wp-content/uploads/2022/01/Screenshot-from-2022-01-29-19-00-35.png)
Matching is done from the start of the string only. If you want to match anywhere use re.search instead:
match = re.match(r"(123)", "a123zzb") match is None # Out: True match = re.search(r"(123)", "a123zzb") match.group() # Out: '123'
ILLUSTRATION (WITH OUTPUT IN NEXT LINE)
![](http://www.fcukthecode.com/wp-content/uploads/2022/01/Screenshot-from-2022-01-29-19-04-47.png)
Morae Q!
- Find the number of strings made by using each alphabet as starting character.
- Find the Pythagorean triplet.
- Find out what is the minimum possible energy he needs to spend.
- Sort the array in non-decreasing order and print out the original indices of sorted array.
- Compute the number of landmasses on the planet after all the meteorites have fallen.
- Give the appropriate server status as output.
- Regular expressions (Regex) using search module in python.
- Find the minimum distance between any pair of equal elements in the array.
- Find the total number of matching pairs of socks that are available.
- Find the total number of teams which can work together and cannot work together.
- Given the heights of all the boys and girls tell whether it is possible for all boys to get a girl.
- Find the sequence of cities to visit according to coordinates and conditions.
- Find the number of unique patches of rectangular land to grow samba(rice) in.
- Regular expression Regex matching strings.
- Generate a greeting quote for admin.
- Find all the cavities on the map and replace their depths with the character X.
- Check whether the given graph is Bipartite or not.
- Find the status of the passengers and safari cars at zoo after k units of time.
- Determine the chair number occupied by the child who will receive that chocolate.
- Check if Rubik’s cube of any dimensions can be assembled.