使用 Javascript 将 Notepad++ 中的参考文件中的 [[Words]] 替换为其他 [[Words]]

如何解决使用 Javascript 将 Notepad++ 中的参考文件中的 [[Words]] 替换为其他 [[Words]]

我有一个看起来像这样的翻译文件

Apple=Apfel
Apple pie=Apfelkuchen
Banana=Banane
Bananaisland=Bananen Insel
Cherry=Kirsche
Train=Zug

...500+ 多行这样的

现在我有一个文件需要处理文本。只需要替换文本的某些部分,例如:

The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]]. 
The [[Apple pie]] tastes great on the [[Bananaisland]].

结果需要

The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]].
The [[Apfelkuchen]] tastes great on the [[Bananen Insel]].

手动复制/粘贴的事件太多了。如上所述,搜索 [[XXX]] 并从另一个文件替换的简单方法是什么?

我尝试为此寻求帮助很多小时,但无济于事。我得到的最接近的是这个脚本:

import re
separators = "=","\n"

def custom_split(sepr_list,str_to_split):
    # create regular expression dynamically
    regular_exp = '|'.join(map(re.escape,sepr_list))
    return re.split(regular_exp,str_to_split)

with open('D:/_working/paired-search-replace.txt') as f:
    for l in f:
        s = custom_split(separators,l)
        editor.replace(s[0],s[1])

不过这样会替换的太多,或者不一致。例如。 [[Apple]] 被 [[Apfel]] 正确替换,但 [[File:Apple.png]] 被 [[File:Apfel.png]] 错误替换,[[Apple pie]] 被 [[Apfel] 替换pie]],所以我尝试连续几个小时调整正则表达式,但无济于事。有没有人有任何信息 - 请用非常简单的术语 - 我如何解决这个问题/实现我的目标?

解决方法

这有点棘手,因为 [ 是正则表达式中的元字符。

我确信有一种更有效的方法可以做到这一点,但这很有效:

replaces="""Apple=Apfel
Apple pie=Apfelkuchen
Banana=Banane
Bananaisland=Bananen Insel
Cherry=Kirsche
Train=Zug"""


text = """
The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]]. 
The [[Apple pie]] tastes great on the [[Bananaisland]].
"""

if __name__ == '__main__':
    import re
    for replace in replaces.split('\n'):
        english,german = replace.split('=')
        text = re.sub(rf'\[\[{english}\]\]',f'[[{german}]]',text)

    print(text)

输出:

The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]]. 
The [[Apfelkuchen]] tastes great on the [[Bananen Insel]].
,

首先,读入带有翻译的文件:

translations={}
with open('file/with/translations.txt','r',encoding='utf-8') as f:
    for line in f:
        items = line.strip().split('=',1)
        translations[items[0]] = items[1]

我假设短语/单词在文件中是唯一的。 然后,您需要匹配 [[]] 之间的所有子字符串,捕获其间的文本(使用像 \[\[(.*?)]] 这样的正则表达式,参见 the online demo),检查是否有键与translations字典中的组1值,如果有这样的键,则替换为[[+字典值+]],如果没有这样的键,则返回整个匹配项翻译:

text = """The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]]. 
The [[Apple pie]] tastes great on the [[Bananaisland]]."""

import re
translated_text = re.sub(r"\[\[(.*?)]]",lambda x: f'[[{translations[x.group(1)]}]]' if x.group(1) in translations else x.group(),text)

输出:

>>> translated_text
'The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]]. \nThe [[Apfelkuchen]] tastes great on the [[Bananen Insel]].'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?