0

为了帮助盲文学习者,我想过滤一个单词列表以仅查找包含字母“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
4

2 回答 2

1

一种简单的方法:

\w*[Dd]\w*[Nn]\w*|\w*[Nn]\w*[Dd]\w*

由于最左边的匹配规则,这个简单的正则表达式应该适用于任何风格。它应该突出整个单词。

如果_和 数字出现在文本中,则将全部更改\w[a-zA-Z]

于 2014-11-03T18:11:42.320 回答
1

这将完全符合您的要求。

^([a-nA-N]*[Dd][a-nA-N]*[Nn][a-nA-N]*|[a-nA-N]*[Nn][a-nA-N]*[Dd][a-nA-N]*)$
#Or for lowercase just:
^([a-n]*[Dd][a-n]*[Nn][a-n]*|[a-n]*[Nn][a-n]*[Dd][a-n]*)$

这是它工作的屏幕截图。

于 2014-11-03T18:34:35.313 回答