正则表达式找到包含' d'和' n'其他字母从a到n

时间:2014-11-03 18:09:24

标签: regex conditional-statements bbedit

为了帮助盲文学习者,我想过滤一个单词列表,只找到那些包含字母'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个匹配:“秃顶”和“乐队”。

bald
balding
bale
baling
balk
balked
balking
balm
bam
ban
band
bane

2 个答案:

答案 0 :(得分:1)

一个简单的方法:

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

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

如果文字中包含_和数字,请将所​​有\w更改为[a-zA-Z]

答案 1 :(得分: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]*)$

Here's a screenshot of it working.

相关问题