正则表达式,正好有2个大写字母和3个数字 [英] Regular Expression with exactly 2 uppercase letters and 3 numbers

查看:747
本文介绍了正则表达式,正好有2个大写字母和3个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要匹配包含完全 2个大写字母和3个数字的单词。数字和大写字母可以位于单词中的任何位置。

I need to match words that contains exactly 2 uppercase letters and 3 numbers. Numbers and uppercase letters can be at any positions in the word.


HelLo1aa2s3d:true

HelLo1aa2s3d: true

WindowA1k2j3:true

WindowA1k2j3: true

AAAsjs21js1:false

AAAsjs21js1: false

ASaaak12:false

ASaaak12: false

我的正则表达式尝试,但只匹配2个大写字母:

My regex attempt, but only matches exactly 2 uppercase letters:

([a-z]*[A-Z]{1}[a-z]*){2}


推荐答案

您可以使用正则表达式 前瞻

You can use regex lookaheads:

/^(?=(?:.*[A-Z].*){2})(?!(?:.*[A-Z].*){3,})(?=(?:.*\d.*){3})(?!(?:.*\d.*){4,}).*$/gm

说明:

^                     // assert position at beginning of line
(?=(?:.*[A-Z].*){2})  // positive lookahead to match exactly 2 uppercase letters
(?!(?:.*[A-Z].*){3,}) // negative lookahead to not match if 3 or more uppercase letters
(?=(?:.*\d.*){3})     // positive lookahead to match exactly 3 digits
(?!(?:.*\d.*){4,})    // negative lookahead to not match if 4 or more digits
.*                    // select all of non-newline characters if match
$                     // end of line
/gm                   // flags: "g" - global; "m" - multiline

Regex101

Regex101

这篇关于正则表达式,正好有2个大写字母和3个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆