In this article, we will learn how to swap Name and Date for each item in a list using Group Capturing and Numeric Back-referencing feature in Regex .
Capturing Group : Parentheses groups the regex between them and captures the text matched by the regex inside them into a numbered group i.e ([\w ]+) which can be reused with a numbered back-reference i.e
We are given a list where each item is a string containing the name of a book followed by it’s release year.
We have to swap the Name and Date for each item in the list using Capturing group in Regex and display the output in a chronological order. Let us consider a few examples:
Examples:
Input : Name (Date)
Output : Date - Name
Input : City of Glass (2009)
Output : 2009 - City of Glass
Input : Pride and Prejudice (1813)
Output : 1813 - Pride and Prejudice
Note: In the output, the date is not enclosed between parenthesis and there is a dash between Date and Name.
Regex_Pattern : ([\w ]+) \((\d{4})\)
Explanation :
- The parenthesis in the above pattern groups the regex between them. ([\w ]+) is the first group capturing Name which is back-referenced by
- (\d{4}) is the second group capturing Date which is back-referenced by
for each string.
- We use .sub() to replace the leftmost non-overlapping occurrences of regex_pattern in the string by the new replacement .
.subl(\g< 2 > - \g< 1 >,string)
|
- Once the Name and Date are swapped, the new string is appended to the new list.
- The new list is then sorted and we traverse through the new list and print each item in it.
Code : Python3 program to swap Name and Date for each item in the list using Regex
import re
def swap_name_date(books = []):
ordered_book_list = []
regex_pattern = re. compile (r '([\w ]+) \((\d{4})\)' )
for book in books:
res = regex_pattern.sub( '\g<2> - \g<1>' ,book)
ordered_book_list.append(res)
ordered_book_list.sort()
for book in ordered_book_list:
print (book)
if __name__ = = "__main__" :
unordered_books_list = [
"City of Glass (2009)" ,
"The Great Gatsby (1925)" ,
"Pride and Prejudice (1813)" ,
"The Hunger Games (2008)" ,
"To Kill a Mockingbird (1960)" ,
"The Notebook (1996)" ,
"Jane Eyre (1847)" ,
"The Catcher in the Rye (1951)" ,
"The Lord of the Rings (1954)" ,
"All the light We Cannot See (2014)" ,
"Immortals of Meluha (2010)"
]
swap_name_date(unordered_books_list)
|
Output:
1813 - Pride and Prejudice
1847 - Jane Eyre
1925 - The Great Gatsby
1951 - The Catcher in the Rye
1954 - The Lord of the Rings
1960 - To Kill a Mockingbird
1996 - The Notebook
2008 - The Hunger Games
2009 - City of Glass
2010 - Immortals of Meluha
2014 - All the light We Cannot See
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!