Change language

Python | Change name and date using group capture in Regex

| |

Capture Group : Parentheses groups the regex between them and captures the text matching the regex inside them into a numbered group, i.e. ([/ w] +), which can be reused with a numbered backlink, ie

g "Group Number" 

We are given a list in which each element is a string with the title of the book, followed by the year of issue. 
We need to swap the Name and Date for each item in the list using the Capture group in Regex and display the output in chronological order. Let’s see some 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 in parentheses, but there is a dash between the date and the name.

Regex_Pattern: ([/ w] +) / ((/ d {4}) /)

Explanation :

  1. The parentheses in the above pattern group the regex between them. ([/ f] +), the first capture group is name , which links back

    g " 1

  2. (/ d {4}) — this is the second date of the group capture, which is backlinked

    g " 2

    for each row.

  3. We use .sub () to replace the leftmost non-overlapping occurrence of regex_pattern in a line with a new replacement.

    . subl (g " 2 " - g " 1 ", string)

  4. Once the name and date are swapped, a new line is added to the new list.
  5. The new list is then sorted and we go through a new list and print each item in it.

Code: Python3 program to replace the name and date for each item in the list with Regex

# Python3 program to change name and date for each item
# in a list using Regex
# sort the list after replacement to display the output
# chronologically

 
# Package to import

import re

  

def swap_name_date (books = []) :

# new empty list to store changed 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 })) ’ )

  

# Iterate over each item in unordered_books_list

# and replace group 2 date with group 1 name

for book in books:

# Result after replacement is saved in res

res = regex_pattern.sub ( ’g "2" - g "1" ’ , book)

  # res added to order_book_list

  ordered_book_list.append (res)

 

# After after all unordered_books_list elements were swapped

# and added to order_book_list

# order_book_list sorted

ordered_book_list.sort ()

 

# Iterate through each item in the order_book_list and print it

for book in ordered_book_list:

print (book)

 
Driver code

if __ name__ = = "__ main__" :

# unordered_books_list stores data for sharing

  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)

Exit :

 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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method