查找以彼此靠近的辅音开头的单词

Find words start with consonants that near to each other

我正在尝试编写一个正则表达式来查找以彼此靠近的辅音开头的单词。

这是我想出的:

\b[^aeiou0-9\W][a-z]+\s[^aeiou0-9\W][a-z]+\b

但问题是我需要检查单词前后。

例如:

Vowels and consonants are sounds, not letters. Depending on your accent and how thinly you slice them, there are about 20 vowels and 24 consonants.

结果:

how thinly 
you slice 

预期结果:

how thinly
thinly you 
you slice 

\b([^aeiou0-9\W][a-z]+)\s(?=([^aeiou0-9\W][a-z]+))

示例:https://regex101.com/r/tSblEq/1

这基于您的模式中的规则。它不是直接匹配 <word><space><word>,而是匹配 <word><space><lookahead word>。然后将 <word><lookahead word> 分别分组到第 1 组和第 2 组。

因为前瞻不消耗字符,所以每个词都针对后续词单独求值。

结果:

Match 1 Group 1: how
Match 1 Group 2: thinly

Match 2 Group 1: thinly
Match 2 Group 2: you

Match 3 Group 1: you
Match 3 Group 2: slice

编辑以更好地演示直接匹配与先行匹配:

直接:

how thinly you slice
         ^

引擎在比赛结束时离开。 'thinly'无法评估。

使用前瞻:

how thinly you slice
    ^     

因为匹配只消耗了“how”和它后面的 space,引擎现在留在单词“thinly”的开头。