我正在尝试格式化一系列测试问题以实施到LibreOffice Writer中的在线课程。测试问题格式最近已更改,在实施之前,我必须在一个文本文件中更新所有500多个问题。
当前问题如下:
1. alter verb (awl-ter)
to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter.
我们需要将格式更改为如下形式:
Word: alter verb (awl-ter)
Definition: to change, become different or modified
Example: The design of the king’s suit was altered to suit his taste.
Possible Exam Question: Can be confused with altar , alter.
我知道足够的正则表达式,能够自动格式化大多数行,但定义行除外。我特别需要在“ Word”行之后立即在行首添加单词“ Definition:”,但我似乎无法弄清楚如何正确执行此操作。
我尝试使用(\b\d+\.\s\w.*[\r\n])
可以找到每个定义组的第一行(如上所示),直到新行/返回-但是,当尝试使用$1Definition:
附加文本时,根据作家的regex帮助文件应保留原始结果并附加“ $ 1”之后的内容,它将替换一次,并且以后再单击“替换”时将不再起作用,或者如果使用“全部替换”按钮...
我添加了$1Definition:
的屏幕截图。搜索似乎确实找到了我要寻找的内容,并且在单击“全部查找”时将突出显示每个集合的第一行。
尝试search function highlighting the string或click "replace" more than once时,问题变成了如上所述的结果。
我目前对什么是问题/如何解决感到困惑。我的Regex似乎在某种程度上破坏了搜索查询和输出。
根据评论,将其写为答案。
对于给定的更改方法...
click "replace all"
要(在第二行中添加“定义:”):
1. alter verb (awl-ter)
to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter.
...虽然LibreOffice Writer的正则表达式似乎不喜欢多行匹配,您可以使用另一个编辑器,例如VSCode ...
1. alter verb (awl-ter)
Definition: to change; become different or modified
■Example: The design of the king’s suit was altered to suit his taste.
□Note: Can be confused with altar , alter.
或括号(Google Brackets.io)...
替换前:
之后:
如果您决定使用其他编辑器,请尝试正则表达式...
并替换为...
(^.*?)([.|\n]+■Example:)
希望有帮助。
使用LibreOffice进行此操作的方法是使用Definition: $1$2
。它旨在处理带有换行符和换行符的正则表达式。
AltSearch extension
然后按全部替换。
Search for: (\b\d+\.\s\w.*)$
Replace: \1\nDefinition:
当我尝试时,默认情况下仅当前选择被选中,因此您可能需要取消选中它。