一个df有2行以下信息
abct\abud\asvd\str\S-1-5-21-2667896552-3076357729-2805140832-1001
esyu\wqeg\vgfg\str\S-1-5-18-2612474552-3076578929-1234567891-107
df = df.replace(r'(?<=str\\).+?(?= )', 'version', regex=True)
我想用上边代码替换str\后面的字符为version,但是并没有成功
abct\abud\abcd\str\version
esyu\wqeg\vgfg\str\version
df['列名'] = df['列名'].apply(lambda x: x[0:x.rindex('\\')+1]+'version')
print(df)
实测有用,请采纳
import re
string_text = r"""
abct\abud\asvd\str\S-1-5-21-2667896552-3076357729-2805140832-1001
esyu\wqeg\vgfg\str\S-1-5-18-2612474552-3076578929-1234567891-107
"""
new_str = ''
str_list = string_text.split('\n')
for i in str_list:
result = re.search(r'S.*', i)
if result:
old_str = result.group()
tmp = i.replace(old_str, 'version')
new_str += tmp + '\n'
print(new_str)