Sitemap
Quantrium.ai

This is Quantrium’s official tech blog. A blog on how technology enables us to develop great software applications for our clients.

QUANTRIUM GUIDES

Extracting Words from a string in Python using the “re” module

7 min readOct 6, 2020

--

Regular Expressions in Python

Using Regular Expressions in Python

import re 

Using "|" Operator to Extract all Occurrence of Specific Words

text = "Chennai is a beautiful city. It’s the capital of the state of Tamil Nadu. Chennai has an area close to 430 kilometer squares. Well chennai is not as large as mumbai which has an area of 603.4 kilometer squares. By road, Chennai is about 1500 kilometers away from Mumbai. Whereas, it is about 2200 kilometers away from Delhi, the capital of India."
cities_record = 'Chennai'
re.findall(cities_record, text)
cities_record = 'Chennai'
re.findall(cities_record, text, flags=re.IGNORECASE)

Searching Multiple Patterns

cities_record = 'Chennai|Mumbai'
re.findall(cities_record, text, flags=re.IGNORECASE)
['Chennai', 'Chennai', 'chennai', 'mumbai', 'Chennai', 'Mumbai']

Extracting Words Containing only Alphabets

text = "\
Banana 1.051 48.25\
Apple 1.024 180.54\
Carrot 0.524 47.20\
Radish 0.251 27.14\
Tomato 0.508 41.05"
words_pattern = '[a-z]+'
re.findall(words_pattern, text, flags=re.IGNORECASE)
['Banana', 'Apple', 'Carrot', 'Radish', 'Tomato']
words_pattern = '[a-zA-Z]+'

Understanding Character Classes in Regex

Extracting Words Followed by Specific Pattern

comment = "This is an great article @Bharath. You have explained the complex topic in a very simplistic manner. @Yashwant, you might find this article to be useful."
username_pattern = '@([a-zA-Z]+)'
re.findall('@([a-zA-Z]+)', comment)
['Bharath', 'Yashwant']
re.findall('@[a-zA-Z]+', comment)
['@Bharath', '@Yashwant']

--

--

Quantrium.ai
Quantrium.ai

Published in Quantrium.ai

This is Quantrium’s official tech blog. A blog on how technology enables us to develop great software applications for our clients.

Bharath Sivakumar
Bharath Sivakumar

Written by Bharath Sivakumar

A Machine Learning enthusiast who wants to make Machine Learning tools accessible to everybody

Responses (1)