我有一组用连字符分隔的ID,最后6个字符包含字母数字值和最后的一些特殊字符,其中只允许使用数字值。 如下所示:
YUIO-10GB-BG4 ==> Should match
U-VI1.1-100-WX-Y9 ==> Should match
1-800-553-6387 ==> Shouldn't match because all are digits
T-Series ==> Shouldn't match as all only 2 letters are capital
我正在尝试下面给出的以下规则,但是面临一些测试查询的困难..
((?=\S{6,})[A-Z]{1,}(([A-Z0-9./+~]+){0,}-){1,}[A-Z0-9./+~]+=*)
答案 0 :(得分:0)
您可以使用以下正则表达式来获取所有匹配项,然后过滤掉那些不包含/[A-Z]/
正则表达式字母的字符:
/(?:^|\s)(?=\S{6,})(?=\S*[A-Z])([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)(?!\S)/g
请参阅regex demo。
<强>详情
(?:^|\s)
- 字符串或空格的开头(?=\S{6,})
- 6个或更多字符(?=\S*[A-Z])
- 在0 +非空白字符后必须至少有1个大写ASCII字母([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)
- 第1组:
[A-Z0-9./+~]+
- 1+大写ASCII字母,数字,.
,/
,+
,~
(?:-[A-Z0-9./+~]+)+
- 出现1次以上:
-
- -
字符[A-Z0-9./+~]+
- 1+大写ASCII字母,数字,.
,/
,+
,~
=*
- 0 + =
个符号(?!\S)
- 字符串的空格或结尾。
参见JS演示:
var s = "1-2-444555656-54545 800-CVB-4=\r\nThe ABC-CD40N= is also supported onslots GH-K on the 4000 Series ISRs using the \r\nXYZ-X-THM . This SM-X-NIM-REW34= information is not captured in the table above \r\nTERMS WITH ONLY DIGITS SHOUD NOT MATCH --> 1-800-553-6387 \r\nNumber of chars less than 6 SHOULD NOT match ---> IP-IP \r\nGH-K\r\nVA-V etc\r\n\r\nFollowing Should match\r\nYUIO-10GB-BG4: Supports JK-X6824-UIO-XK++= U-VI1.1-100-WX-Y9\r\nXX-123-UVW-3456\r\nVA-V-W-K9\r\nVA-V-W\r\n\r\nThe following term is not matching as there is no Alphabet in first term-----------> 800-CVB-4= \r\nThis should match\r\n\r\nCD-YT-GH-40G-R9(=) \r\nCRT7.0-TPS8K-F\r\nJ-SMBYTRAS-SUB=\r\n===============================\r\n\r\nBelow terms should NOT match\r\nGH-K\r\nVA-V-W\r\nST-M UCS T-Series <-- Should NOT match\r\n\r\n";
var m, res=[];
var rx = /(?:^|\s)(?=\S{6,})(?=\S*[A-Z])([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)(?!\S)/g;
while(m=rx.exec(s)) {
res.push(m[1]);
}
console.log(res);