Regex probem for strings inputs

Question:

I am looking in python to find the most suitable regex for my examples. I would like to apply one regex on all my inputs to get all my desired outputs


# examples of inputs
chaine1 = "5.12E: Regulation of the Calvin Cycle"
chaine2 = "2. 12: Diffusion"
chaine3 = "3.4.5: Principal Axes of Inertia"
chaine4 = "9.3: Van Der Waals Forces between Atoms"

# examples of outputs
expect_result_1="Regulation of the Calvin Cycle"
expect_result_2="Diffusion"
expect_result_3="Principal Axes of Inertia"
expect_result_4="Van Der Waals Forces between Atoms"

I tried different schemas of regex but did not succeed in it

Asked By: DataSciRookie

||

Answers:

You can use this Regex for your expected outputs from given inputs.

r'^.*?:s*(.*)$'

Complete working code :

import re

chaine1 = "5.12E: Regulation of the Calvin Cycle"
chaine2 = "2. 12: Diffusion"
chaine3 = "3.4.5: Principal Axes of Inertia"
chaine4 = "9.3: Van Der Waals Forces between Atoms"

regex_pattern = r'^.*?:s*(.*)$'

chaine1 = re.sub(regex_pattern, r'1', chaine1)
chaine2 = re.sub(regex_pattern, r'1', chaine2)
chaine3 = re.sub(regex_pattern, r'1', chaine3)
chaine4 = re.sub(regex_pattern, r'1', chaine4)

print(chaine1)
print(chaine2)
print(chaine3)
print(chaine4)

Output:

Regulation of the Calvin Cycle
Diffusion
Principal Axes of Inertia
Van Der Waals Forces between Atoms
Answered By: NIKUNJ PATEL
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.