Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex: Remove a pattern at the end of string

Tags:

python

regex

Input: blah.(2/2)

Desired Output: blah

Input can be "blah.(n/n)" where n can be any single digit number.

How do I use regex to achieve "blah"? This is the regex I currently have which doesn't work:

m = re.sub('[.[0-9] /\ [0-9]]{6}$', '', m)
like image 440
90abyss Avatar asked Oct 02 '15 18:10

90abyss


2 Answers

You need to use regex \.\(\d/\d\)$

>>> import re
>>> str = "blah.(2/2)"
>>> re.sub(r'\.\(\d/\d\)$', '',str)
'blah'

Regex explanation here

Regular expression visualization

like image 91
Pranav C Balan Avatar answered Oct 02 '22 16:10

Pranav C Balan


Just do this since you will always be looking to split around the .

s = "stuff.(asdfasdfasdf)"
m = s.split('.')[0]
like image 37
idjaw Avatar answered Oct 02 '22 16:10

idjaw