import re
text = 'tls_0925_13")'
pattern = re.compile(r'\w\d+"')
s = str(re.findall(pattern, text))
print(s) #Output ['_13"]
k = text.split(s)
print(k) #Output ['tls_0925_13");']
tls_0925 I wanted to extract this part, so I practiced the above part I can't split it. Is there a way?
python split
>>> text = 'tls_0925_13")'
>>> text2 = '_'.join(text.split('_')[:2])
>>> text2
'tls_0925'
If you want to write str objects in the list, you must join or use the list index.
s = ''.join(re.findall(pattern, text))
Or
s = re.findall(pattern, text)[0]
k = text.split(s)
© 2024 OneMinuteCode. All rights reserved.