douxingti9307 2015-03-22 13:28
浏览 137
已采纳

正则表达式捕获至少有一个小写字母的单词

I'm trying to capture words in a string like:

1vTvFpU
KOoy6Cc

With regex pattern:

\b(?=(?:.*?[a-z]){1,})[A-Za-z0-9\/\-_.]{7,7}\b

But I have a problem because it also matches words like:

FDSFDFI
WEWEFDP
RRRRRRR

In a string:

FDSFDFI sdfdfdf
WEWEFDP traliii
RRRRRRR sdfdfdf

What Am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dpsr1670 2015-03-22 13:30
    关注

    I suggest you to use \S* instead of .* inside the lookahead. Because when you include .*? inside the lookahead, it checks for atleast one lower-case letter for the whole line not for the word.

    \b(?=(?:\S*?[a-z]))[A-Za-z0-9\/\-_.]{7}\b
    

    {7,7} is equal to {7}

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?