你确定这个正则表达式没问题?为什么我一个都匹配不到
这是我的古董文本编辑器支持的js脚本,跑过没问题。你确定 python 里 RegExp() 这个函数吗?
RegExp对应的是Python里的re.compile函数?那里我不编译成正则表达式模式行不行?还有你这个代码是在什么操作系统下跑通的?我在Windows里面是不是应该把\r\n换成\n?另:在你的设定中,.能不能匹配到换行符?
import re
code = input("请输入编码方式:")
source = open('input.txt', 'r', encoding=code)
content = source.read()
source.close()
content = "\n" + content + "\n" # 当前文本文件的完整内容
myEntry = "" # 一个词头
myContent = "" # 一条释义
while re.findall(r'\n@@@LINK=.+\n', content, re.S):
myEntry = re.findall(r'\n@@@LINK=.+\n', content,re.S)[0]
myEntry = re.sub(r'@@@LINK=', '', myEntry)
myEntry = re.sub(r'\n', '', myEntry)
myContent = re.findall('\n' + myEntry + '\n.+\n', content, re.S)[0] # 这行的正则表达式有误
myContent = re.sub('\n' + myEntry + '\n', '\n', myContent)
content = re.sub('\n@@@LINK=.+\n', myContent, content)
output_file = open('output.txt', 'w+', encoding=code)
output_file.write(content)
output_file.close()
这我不知道,我想那点代码虽然是js的,但是意思应该一看就懂吧
看来你没找到python里面生成包含字符串变量的正则表达式的函数
在 Windows 下的一个小众古董编辑器中的 js 脚本
我碰到的大多数文件都是用 \r\n 就能匹配换行符,有些可能只要 \r 或 \n,这里面有个说法的现在忘了
测试