为了帮助盲文学习者,我想过滤一个单词列表以仅查找包含字母“d”和“n”的单词。我正在使用 BBEdit 10.5.13 中的正则表达式引擎。我有一个文件包含一个单词列表,每行一个单词。
这是一个匹配每一行的正则表达式,这当然不是我想要的。
\w*?(d)?(n)?(?(1)\w*?n|(?(2)\w*?d))\w*
我想象的逻辑是:
\w*? Match all the letters before the first 'd' or 'n', if there are any
(d)? If there is a 'd' before the first 'n', capture it
(n)? If there is an 'n' before the first 'd', capture it
(?(1) If a 'd' was captured...
\w*?n ... then match all characters up to the first 'n'
|(?(2) Else if an 'n' was captured...
\w*?d ... then match all characters up to the first 'd'
))\w* Continue the match until the end of the word
显然,我的逻辑和我的正则表达式的逻辑是不同的,因为这匹配每个单词,无论它是否包含“d”或“n”。任何纠正我的逻辑的帮助将不胜感激。
这是列表中的简短摘录,包含所需的 2 个匹配项:“balding”和“band”。
bald
balding
bale
baling
balk
balked
balking
balm
bam
ban
band
bane