You can use the isalpha()
function in a generator expression. Then combine the characters as a single string
using join()
.
def extract_string(s):
return ''.join(i for i in s if i.isalpha())
Sample output:
print extract_string(':::ABC???,:::DEF???')
>>> ABCDEF
However that is only for extracting all characters, if you want to extract only characters between ~...^
:
import re
def extract_string(s):
match = re.findall(r"~([a-zA-z]*)\^", s)
return match
Sample output:
s = ' ~ABC^,~DEF^'
print extract_string(s)
>>> ['ABC', 'DEF']
Just a side note: if you're parsing HTML using regex and/or string manipulation, as the famous S.O. reply suggests, please use a HTML parser; such as the Beautiful Soup library instead :D!