Getting started with regex in python

I have been wanting to learn regex. Not just because it reminds me of a bit but because it’s actually very useful and can be used within numerous languages including python. In case you don’t know, regex stands for regular expression. A regex is “a sequence of characters that define a search pattern.” Most popular use case is to search strings for a pattern.

I was looking at videos from this year’s PyCon and stumbled up on a video of Trey Hunner conducting regex workshop at PyCon 2016. If you are looking to learn regex, I encourage you to watch the video. You can also find most of the stuff mentioned in the video on this website.

After I watched the video, I did some practice on my own which I wanted to share here.

This implies that the Kamagra viagra cheap no prescription jelly can provide you with a better result and happy sexual life. see these guys sildenafil 100mg uk Male impotency has been a common issue among the males has been reported as a significant factor for their broken relationship. viagra 50mg price Still, the ED patients can buy this drug by using sildenafil citrate as a key ingredient. Thereby you can’t perform well in the lovemaking activities. find out this order levitra online

In [9]:
# Import the regex module
import re 

# Define our sentence
sentence = "I am a 26 years old software developer and I used to work on Wall Street (10005)."

Get the first digit from the sentence

In [4]:
re.search(r'\d', sentence).group()
Out[4]:
'2'

Get the first non-digit from the sentence

In [5]:
re.search(r'\D', sentence).group()
Out[5]:
'I'

Search for a substring in the sentence

In [6]:
re.search(r'years', sentence).group()
Out[6]:
'years'
In [8]:
re.search(r'heart', sentence)  # Doesn't return anything since our sentence doesn't have 'heart'

Get the first character from the sentence

In [10]:
re.search(r'\w', sentence).group()
Out[10]:
'I'

Get the first word with 3 or more characters from the sentence

In [13]:
re.search(r'\w{3,}', sentence).group()
Out[13]:
'years'

Get the first word with 3 or less characters from the sentence

In [14]:
re.search(r'\w{,3}', sentence).group()
Out[14]:
'I'

Get the zip code from the sentence (any 5 consecutive digits)

In [15]:
re.search(r'\d{5}', sentence).group()  # Get 5 consecutive digits
Out[15]:
'10005'

Search for 'wall' in the sentence

In [17]:
re.search(r'wall', sentence) # doesn't return anything, need to ignore case
In [21]:
re.search(r'wall', sentence, re.IGNORECASE).group()
Out[21]:
'Wall'

Split the sentence at 'and'

In [39]:
re.split(r'and', sentence)
Out[39]:
['I am a 26 years old software developer ',
 ' I used to work on Wall Street (10005).']

Leave a comment

Your email address will not be published. Required fields are marked *