正则表达式缺少一个项目

时间:2018-01-23 22:57:53

标签: regex python-3.x regex-group

我有这个文字

System Name: (.*)((.stc.com.sa)|(-re0_NOPAT)|(-re0))

我正在使用这个正则表达式

arr1 = ["Precon", "Contra", "Postco", "Cancel", "Consul"] arr2 = ["EJID", "EMBA", "EMPR", "GOBI", "PART", "PPOL", "SACI", "SOFL", "SOFM", "0000", "", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009", "0010", "0011", "0012", "0013", "0014", "0015", "0016", "011", "0110", "9999"] var arr = []; console.log(concat(arr1, arr2, 0, 0, 0)); function concat(arr1, arr2, arrIndex, index1, index2) { //console.log(arr1.length); if (index2 === (arr2.length)) { return; } else { arr[arrIndex] = arr1[index1] + '-' + arr2[index2]; if (index1 !== (arr1.length - 1)) { index1++; } else if (index1 === (arr1.length - 1)) { index1 = 0; index2++; //concat(arr1, arr2, ++arrIndex, index1, index2++); // Not here dummy :P } concat(arr1, arr2, ++arrIndex, index1, index2++); } return arr; }

系统名称缺少1个字符串:ProCurve 6120G / XG刀片式交换机

2 个答案:

答案 0 :(得分:1)

您可以使用

(?m)System Name: (.*?)(?=\.stc\.com\.sa|-re0_NOPAT|-re0|$)

请参阅regex demo

<强>详情

  • (?m) - 使$匹配行尾
  • 的多行修饰符
  • System Name: - 文字子字符串
  • - 空格
  • (.*?)(?=\.stc\.com\.sa|-re0_NOPAT|-re0|$) - 第1组:除了换行符之外的任何0 +字符,尽可能少,直到第一次出现:
    • .stc.com.sa
    • -re0_NOPAT
    • -re0
    • $ - 行尾。

答案 1 :(得分:0)

您的可选组都不匹配该字符串。尝试这样的事情:

System Name: (.*)((.stc.com.sa)|(-re0_NOPAT)|(-re0)|(Blade Switch))

这将呈现:ProCurve 6120G/XG

您与最后一组匹配的字符串的哪一部分取决于您可能要过滤的其他值。

如果您不想删除任何内容,只有System Name:,您可以将最后一组组设为可选:

System Name: (.*)((.stc.com.sa)|(-re0_NOPAT)|(-re0)|(Switch))?

这将呈现:ProCurve 6120G/XG Blade Switch

相关问题