Skip to content Skip to sidebar Skip to footer

Removing The Unwanted Substrings From A Series Of Strings

I have a series of strings as given below Tata Jaguor 1474 pSNL Series Car Tata Nano Pro 5864 Series Car Tata Indica 8586 k5478 Tata Nano 5864 E5478 Tata Bolero 8974 1567 Series A

Solution 1:

reobj = re.compile(r"Tata ([\w ]+\d+).*?$", re.IGNORECASE | re.MULTILINE)
result = reobj.sub(r"\1", subject)

http://rubular.com/r/jvvtCjlTKy

Jaguor1474NanoPro5864Indica8586 k5478Nano5864 E5478Bolero8974 1567

Solution 2:

Following is the regular expression for you:

'\s.*[0-9]{4}'

You can implement in python. This isnt removing what you are 'not' looking for instead giving what you are looking for.

Solution 3:

In conjunction with my comment:

In your example, you have Tata but you are trying to replace Tata<space><space>. series in your example is written differently and the same goes for Pro, digging the documentation for the String replace()

The method replace() returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.

In your case, you do not seem to be doing anything with the returned value, this should work:

Vehiclename = Vehiclename.replace("Tata ","").replace("Series","").replace("Pro ","").replace(" Car","")

Post a Comment for "Removing The Unwanted Substrings From A Series Of Strings"