How to remove a character from a string until certain index? [duplicate] - python

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

Related

How to remove all characters within a string after a non-alphanumerical character [duplicate]

This question already has answers here:
How to remove all characters after a specific character in python?
(10 answers)
Closed 1 year ago.
I want to remove all the character after a non-alphanumerical character ('_') within a string.
For example:
Petr_;Y -> Petr
ČEZ_^(České_energetické_závody) -> ČEZ
I tried:
''.join(c for c in mystring if c.isalnum())
But this way I'm stripping off only alphanumerical characters itself.
Help would be appreciated.
You may want to use the .split() method on strings.
new_string = your_string.split('_',1)[0]
This way you keep only what's before the fisrt '_'.
Searching the index of first occurrence of "_" will do:
s1 = "Petr_;Y"
s2 = "ČEZ_^(České_energetické_závody)"
s11 = s1[:s1.index("_")]
s22 = s2[:s2.index("_")]

Split string on "$" using regex [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 1 year ago.
I am trying to split a string using regex on $ symbol but the output is not what I want.
string = "43$hello"
list_of_splits = re.split("$",string)
Output:
['43$hello','']
Output I want:
['43','hello']
It's visible by the output that "$" is a special character in regex, but now by how can I do this?
Use the escape character \ : list_of_splits = re.split("\$", str)
You can just use string split method.
string = "43$hello"
string.split("$")
Output
['43', 'hello']

Capture repeated characters and split using Python [duplicate]

This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 3 years ago.
I need to split a string by using repeated characters.
For example:
My string is "howhowhow"
I need output as 'how,how,how'.
I cant use 'how' directly in my reg exp. because my input varies. I should check the string whether it is repeating the character and need to split that characters.
import re
string = "howhowhow"
print(','.join(re.findall(re.search(r"(.+?)\1", string).group(1), string)))
OUTPUT
howhowhow -> how,how,how
howhowhowhow -> how,how,how,how
testhowhowhow -> how,how,how # not clearly defined by OP
The pattern is non-greedy so that howhowhowhow doesn't map to howhow,howhow which is also legitimate. Remove the ? if you prefer the longest match.
lengthofRepeatedChar = 3
str1 = 'howhowhow'
HowmanyTimesRepeated = int(len(str1)/lengthofRepeatedChar)
((str1[:lengthofRepeatedChar]+',')*HowmanyTimesRepeated)[:-1]
'how,how,how'
Works When u know the length of repeated characters

Removing contents within brackets in string [duplicate]

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.

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Categories

Resources