Monday, January 25, 2021

Regular Expressions | Grouping

Regex: Grouping::

Suppose we have textual data with dates in it and we want to extract only the year from the dates. This can be done using regular expression pattern with grouping to match dates and then we can extract the component elements such as the day, month or the year from the date.


Example:

url = "http://www.telegraph.co.uk/formula-1/2017/10/28/mexican-grand-prix-2017-time-does-start-tv-channel-odds-lewisl/2017/05/12"

date_regex = '/(\d{4})/(\d{1,2})/(\d{1,2})/'

print(re.findall(date_regex, url))




No comments:

Post a Comment

Element wise operation on LIST vs ARRAY

The use of arrays over lists: You can write  vectorised  code on numpy arrays, not on lists, which is  convenient to read and write, and con...