Python 正则表达式代码,用于删除由“分隔”分隔的字符串+++$+++&引用;从文本文件?
我有一个文本文件,我想从中删除Python 正则表达式代码,用于删除由“分隔”分隔的字符串+++$+++&引用;从文本文件?,python,regex,Python,Regex,我有一个文本文件,我想从中删除++$++ 以下是文件: L666256 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ Colonel Durnford... William Vereker. I hear you 've been seeking Officers? L666257 +++$+++ u9030 +++$+++ m616 +++$+++ DURNFORD +++$+++ Good ones, yes, Mr Vereker.
++$++
以下是文件:
L666256 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ Colonel Durnford... William Vereker. I hear you 've been seeking Officers?
L666257 +++$+++ u9030 +++$+++ m616 +++$+++ DURNFORD +++$+++ Good ones, yes, Mr Vereker. Gentlemen who can ride and shoot
L666369 +++$+++ u9030 +++$+++ m616 +++$+++ DURNFORD +++$+++ Your orders, Mr Vereker?
L666370 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ I'm to take the Sikali with the main column to the river
L666371 +++$+++ u9030 +++$+++ m616 +++$+++ DURNFORD +++$+++ Lord Chelmsford
seems to want me to stay back with my Basutos.
L666372 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ I think Chelmsford wants a good man on the border Why he fears a flanking attack and requires a steady Commander in reserve.
例如,我希望第一行输出为collone Durnford。。。威廉·韦里克。听说你一直在找警官?
而不是当前字符串<代码>L666256++$+++u9034++$+++m616++$+++++VEREKER++$+++Durnford上校。。。威廉·韦里克。听说你一直在找警官?
我该怎么做?读取文本文件并使用字符串方法替换确切的字符 例如:
txt = """L666256 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ Colonel Durnford... William Vereker. I hear you 've been seeking Officers?"""
print(' '.join(txt.split(" +++$+++ ")))
或者只需使用replace()
或者使用正则表达式和re模块
string = "L666256 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ Colonel Durnford... William Vereker. I hear you 've been seeking Officers?"
regex = r"^.*\+{3}\$\+{3}\s*"
re.sub(regex, "", string)
那不是“写我的软件”服务吗?你确定你需要正则表达式吗<代码>拆分()可能就足够了。试试看
string = "L666256 +++$+++ u9034 +++$+++ m616 +++$+++ VEREKER +++$+++ Colonel Durnford... William Vereker. I hear you 've been seeking Officers?"
regex = r"^.*\+{3}\$\+{3}\s*"
re.sub(regex, "", string)