Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Swap Name and Date using Group Capturing in Regex

Python | Swap Name and Date using Group Capturing in Regex

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




\g<Group Number>


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 :

  1. The parenthesis in the above pattern groups the regex between them. ([\w ]+) is the first group capturing Name which is back-referenced by




    \g<1>

    
    
  2. (\d{4}) is the second group capturing Date which is back-referenced by




    \g<2>

    
    

    for each string.

  3. 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)

    
    
  4. Once the Name and Date are swapped, the new string is appended to the new list.
  5. 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




# Python3 program to swap Name and Date for each item
# in the list using Regex
# sort the list after swapping to display the output
# in a chronological order
  
#Importing re package
import re
  
def swap_name_date(books=[]):
    # new empty list to store swapped data
    ordered_book_list=[]
  
    # RegexObject = re.compile( Regular expression, flag )
   # Compiles a regular expression pattern into a regular expression object
    regex_pattern = re.compile(r'([\w ]+) \((\d{4})\)')
  
    # Iterating through each item in unordered_books_list    
   # and swapping group 2 of date with group 1 of name
    for book in books:
        # Result after swapping is stored in res
        res = regex_pattern.sub('\g<2> - \g<1>' ,book)
        # res is appended to ordered_book_list
        ordered_book_list.append(res)
  
    # After all items of unordered_books_list are swapped
        # and appended to ordered_book_list
    # ordered_book_list is sorted
    ordered_book_list.sort()
  
    # Iterating through each item of ordered_book_list and printing it
    for book in ordered_book_list:
        print(book)
  
# Driver Code
if __name__=="__main__":
    #unordered_books_list stores data to be swapped
    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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments