This question already has answers here:
replace string in pandas dataframe
(3 answers)
Closed 6 years ago.
I want to remove the brackets and contents withing the brackets from a string.
I tried following code:
a['Street Name'].str.replace('\(.*)','')
But it is not working. Can anybody please tell me what is wrong with this statement?
Try this:
import re
s = "I want to remove all words in brackets( like (this) and ((this)) and ((even) this))."
while True:
s_new = re.sub(r'\([^\(]*?\)', r'', s)
if s_new == s:
break
s = s_new
print(s_new) # I want to remove all words in brackets.
Related
This question already has answers here:
How do I coalesce a sequence of identical characters into just one?
(10 answers)
Closed 2 years ago.
I have a string, something like that (I don't know in advance how much similar characters in a sequence):
s = '&&&&&word&&&word2&&&'
and would like to obtain as a result this string:
'&word&word2&'
Workaround is something like this (not effective I guess for large texts):
while True:
if not '&&' in s:
break
s = s.replace('&&','&')
You can use a regex to replace any occurence of one or more '&' (&+) by '&':
import re
s = '&&&&&word&&&word2&&&'
res = re.sub(r'&+', '&', s)
print(res)
# &word&word2&
This question already has answers here:
Escaping regex string
(4 answers)
Closed 3 years ago.
ı am trying to stemmize words in tex of dataframe
data is a dataframe , karma is text column , zargan is the dict of word and root of word
for a in range(1,100000):
for j in data.KARMA[a].split():
pattern = r'\b'+j+r'\b'
data.KARMA[a] = re.sub(pattern, str(zargan.get(j,j)),data.KARMA[a])
print(data.KARMA[1])
I want to change the word and root in the texts
Looks like j contains some regular expression special character like *. If you want it to be interpreted as literal text, you can say
pattern = r'\b'+re.escape(j)+r'\b'
and possibly the same for r if it should similarly be coerced into a literal string.
This question already has answers here:
How can I remove text within parentheses with a regex?
(9 answers)
Closed 4 years ago.
LIST = ['ichenbsdr1.chen.com', 'ichenbsds1(SSI15170CCD)',
'ichenbsds1', 'ichenbsdm2.chen.com',
'ichenbsdm2.chen.com(ABQB344DEGH)', 'ichenbsdm2']
Need to filter using regex on above list. whichever the index got
brackets need to be removed with the information. LIST[1] is
'ichenbsds1(SSI15170CCD)', have to remove "(SSI15170CCD)" and show
'ichenbsds1' alone same as in LIST[4] as well.
I have this regex r'(.*?)\(.*\)' to remove brackets and whatever
present inside those brackets. But when i run in the below script its
not giving exact output.
sws=[]
for line in LIST:
Type = re.search(r'(.*?)\(.*\)', line)
sws.append(Type)
print (sws)
Expected Output:
['ichenbsdr1.chen.com', 'ichenbsds1', 'ichenbsds1', 'ichenbsdm2.chen.com', 'ichenbsdm2.chen.com', 'ichenbsdm2']
Use re.sub to remove everything between parenthesis
>>> [re.sub(r'\(.*?\)', '', s) for s in LIST]
['ichenbsdr1.chen.com', 'ichenbsds1', 'ichenbsds1', 'ichenbsdm2.chen.com', 'ichenbsdm2.chen.com', 'ichenbsdm2']
This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.
This question already has answers here:
Remove characters from beginning and end or only end of line
(5 answers)
Closed 4 years ago.
So, I have the following string "........my.python.string" and I want to remove all the "." until it gets to the first alphanumeric character, is there a way to achieve this other than converting the string to a list and work it from there?
You can use re.sub:
import re
s = "........my.python.string"
new_s = re.sub('^\.+', '', s)
print(new_s)
Output:
my.python.string