Big_Data
Big_Data

Reputation: 322

remove special charecters in a string using python

I have a string = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg" with special characters.

what i am trying is to remove all special charecters in the string and then display the word 'Aroha Technologies'

i was able to do with hard coding using lstrip() function but can anyone help me out how can i display string 'Aroha Technologies' in a single line using regular expressions.

edit suggested:-

by using this lstrip() and rstrip() functions i was able to remove characters from the string.

str = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg"

str=str.lstrip('msdjdgf(^&%*(')

str=str.rstrip('&^$^&*^CHJdjg')

Upvotes: 1

Views: 170

Answers (2)

ysth
ysth

Reputation: 98388

You don't provide a lot of information, so this may or may not be close to what you want:

import re
origstr = "msdjdgf(^&%(Aroha Technologies&^$^&^CHJdjg"
match = re.search("[A-Z][a-z]*(?: [A-Z][a-z]*)*", origstr)
if match:
    newstr = match.group()

(looks for a series of capitalized words with spaces between them)

Upvotes: 2

P.hunter
P.hunter

Reputation: 1365

here, A bit more dirty approach

import re # A module in python for String matching/operations  
a = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg"
stuff = re.findall('\W(\w+\s\w+)\W', a)
print(stuff[0]) # Aroha Technologies

hope this helps ;)

Upvotes: 3

Related Questions