擅长:python、mysql、java
<p>模式的最后一部分<code>(?=.*[a-z]){8,32}</code>对小写字符重复肯定的前瞻断言8-32次。你知道吗</p>
<p>如果希望第三个单词的长度为8-32个非空白字符长,可以使用<code>\S{8,32}(?!\S)</code></p>
<p>在您的模式中,您实际上没有匹配第三个单词。你知道吗</p>
<p>在lookaheads中,您可以使用否定,利用<a href="https://www.regular-expressions.info/charclass.html#negated" rel="nofollow noreferrer">negated character class</a>添加一个空白字符来不跨越边界。你知道吗</p>
<p>您可以使用:</p>
<pre><code>\bword1(?:[ \t]+\S+)*\W+word2\b.*?\s+(?=[^\d\s]*\d)(?=[^A-Z\s]*[A-Z])(?=[^a-z\s]*[\sa-z])\S{8,32}(?!\S)
</code></pre>
<p><a href="https://regex101.com/r/LnxFYP/1" rel="nofollow noreferrer">Regex demo</a>| <a href="https://ideone.com/LKReh3" rel="nofollow noreferrer">Python demo</a></p>
<p>你的代码可能看起来像</p>
<pre><code>import re
def findWordandCode(w1, w2, sentence):
return re.search(r'\b{0}(?:[ \t]+\S+)*\W+{1}\b.*?\s+(?=[^\d\s]*\d)(?=[^A-Z\s]*[A-Z])(?=[^a-z\s]*[\sa-z])\S{{8,32}}(?!\S)'.format(w1, w2), sentence)
res = findWordandCode('word1','word2','word1 word2, abcABC123! blah blah')
if res:
print(res.group())
</code></pre>
<p>结果</p>
<pre><code>word1 word2, abcABC123!
</code></pre>