当前位置: 首页 > 知识库问答 >
问题:

由连字符分隔的字母数字正则表达式模式

洪楷
2023-03-14

我有一组由连字符分隔的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./+~]+=*)

https://regex101.com/r/d8MFRE/5

共有1个答案

乌俊健
2023-03-14

您可以使用以下正则表达式来获取所有匹配项,然后过滤掉那些不包含带有正则表达式的字母的匹配项:

/(?:^|\s)(?=\S{6,})(?=\S*[A-Z])([A-Z0-9./+~]+(?:-[A-Z0-9./+~]+)+=*)(?!\S)/g

请参阅正则表达式演示。

详细信息

  • (?:^ |\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字母,数字, /~
      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);

 类似资料: