Regular Expressions

Regular expressions, also known as regex, are a powerful language for matching and parsing strings. They are represented as special text strings which describe a search pattern. Regular expressions can be used for tasks such as finding specific words in a document, validating user inputs, or reformatting text data. Python's re module provides support for regular expressions.

Search for a substring (first occurrence)

One of the most common uses for regular expressions is the search function. This function searches the string for a match to a pattern and returns a match object if there is a match. If there is more than one match, only the first occurrence is returned. Let's look at an example:
import re

text = 'The quick brown fox jumps over the lazy dog'
res ='quick', text)

print(res)  # <re.Match object; span=(4, 9), match='quick'>
In this example, we searched for the string quick in our text. As you can see, the search function returned a match object, which tells us that the match starts at index 4 and ends at index 9 in the text.

Find all substrings

The re module also provides a function called findall which, unlike search, returns all matches of a pattern in a string as a list of strings. The string is scanned from left to right, and matches are returned in the order they're found. If no matches are found, an empty list is returned. For example:
import re

text = 'The quick brown fox jumps over the quick dog'
matches = re.findall('quick', text)

print(matches)  # ['quick', 'quick']
Here, findall found two occurrences of 'quick' in our text, so it returned a list containing 'quick' twice.
Regular expressions aren't limited to plain text. We can also use special sequences, also known as metacharacters, to find patterns in text. For instance, \d matches any digit (0-9). We will discuss metacharacters in more depth later. Let's see an example:
import re

text = '123 abc 456 def'
matches = re.findall('\\d', text)   # \d matches any digit
print(matches)                      # ['1', '2', '3', '4', '5', '6']

matches = re.findall('\\d+', text)  # \d+ matches 1 or more consecutive digits
print(matches)                      # ['123', '456']
In this case, we used '\d' to find all digits in our text. The findall function returned a list of all the digits in the order they were found.
As you can see, the re module in Python provides a powerful way to search, match, and extract patterns in text.

Challenge: Find #Numbers

Your task is to create a program that uses regular expressions to identify and print hashtag-prefixed (#) numbers from an input text.
Given a single line in an input, the program should output all the hashtag-prefixed numbers, each on a new line.
Hello #12343. Call #98765. Note #7865. Another random number is 132.
#12343 #98765 #7865
Note: Only numbers directly prefixed with a hashtag should be considered.


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue