Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 匹配中的vbscript匹配_Regex_Vbscript_Pattern Matching - Fatal编程技术网

Regex 匹配中的vbscript匹配

Regex 匹配中的vbscript匹配,regex,vbscript,pattern-matching,Regex,Vbscript,Pattern Matching,大家好 我正在路由器上运行一些Cisco show命令。我正在将输出捕获到一个数组中。我想使用正则表达式在输出中查找某些信息。正则表达式的工作原理是找到包含它的行,但是没有足够的唯一信息可以创建正则表达式,因此我最终得到了更多想要的信息。以下是输出: ROUTERNAME#sh diag Slot 0: C2821 Motherboard with 2GE and integrated VPN Port adapter, 2 ports Port adapte

大家好

我正在路由器上运行一些Cisco show命令。我正在将输出捕获到一个数组中。我想使用正则表达式在输出中查找某些信息。正则表达式的工作原理是找到包含它的行,但是没有足够的唯一信息可以创建正则表达式,因此我最终得到了更多想要的信息。以下是输出:

 ROUTERNAME#sh diag 
Slot 0:
        C2821 Motherboard with 2GE and integrated VPN Port adapter, 2 ports
        Port adapter is analyzed 
        Port adapter insertion time 18w4d ago
        Onboard VPN             : v2.3.3
        EEPROM contents at hardware discovery:
        PCB Serial Number        : FOC1XXXXXXXXX
        Hardware Revision        : 1.0
        Top Assy. Part Number    : 800-26921-04
        Board Revision           : E0
        Deviation Number         : 0
        Fab Version              : 03
        RMA Test History         : 00
        RMA Number               : 0-0-0-0
        RMA History              : 00
        Processor type           : 87 
        Hardware date code       : 20090816
        Chassis Serial Number    : FTXXXXXXXXXX
        Chassis MAC Address      : 0023.ebf4.5480
        MAC Address block size   : 32
        CLEI Code                : COMV410ARA
        Product (FRU) Number     : CISCO2821      
        Part Number              : 73-8853-05
        Version Identifier       : V05 
        EEPROM format version 4
        EEPROM contents (hex):
          0x00: 04 FF C1 8B 46 4F 43 31 33 33 33 31 4E 36 34 40
          0x10: 03 E8 41 01 00 C0 46 03 20 00 69 29 04 42 45 30
          0x20: 88 00 00 00 00 02 03 03 00 81 00 00 00 00 04 00
          0x30: 09 87 83 01 32 8F C0 C2 8B 46 54 58 31 33 33 36
          0x40: 41 30 4C 41 C3 06 00 23 EB F4 54 80 43 00 20 C6
          0x50: 8A 43 4F 4D 56 34 31 30 41 52 41 CB 8F 43 49 53
          0x60: 43 4F 32 38 32 31 20 20 20 20 20 20 82 49 22 95
          0x70: 05 89 56 30 35 20 D9 02 40 C1 FF FF FF FF FF FF

AIM Module in slot: 0
        Hardware Revision        : 1.0
        Top Assy. Part Number    : 800-27059-01
        Board Revision           : A0
        Deviation Number         : 0-0
        Fab Version              : 02
        PCB Serial Number        : FOXXXXXXXXX
        RMA Test History         : 00
        RMA Number               : 0-0-0-0
        RMA History              : 00
        Product (FRU) Number     : AIM-VPN/SSL-2
        Version Identifier       : V01
        EEPROM format version 4
        EEPROM contents (hex):
          0x00: 04 FF 40 04 F4 41 01 00 C0 46 03 20 00 69 B3 01
          0x10: 42 41 30 80 00 00 00 00 02 02 C1 8B 46 4F 43 31
          0x20: 33 33 31 36 39 59 55 03 00 81 00 00 00 00 04 00
          0x30: CB 8D 41 49 4D 2D 56 50 4E 2F 53 53 4C 2D 32 89
          0x40: 56 30 31 00 D9 02 40 C1 FF FF FF FF FF FF FF FF
          0x50: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
          0x60: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
          0x70: FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
我要捕获的是“产品(FRU)编号:”部分中包含的型号。在本例中为“CISCO2821”。我想输出或MsgBox只是CISCO2821,尽管其他可能是“CISCO2911/K9”或类似的东西

这是我正在使用的正则表达式模式:

Product\s\(FRU\)\sNumber\s*:\s*CIS.*
使用正则表达式测试工具,我能够匹配包含我想要的内容的整行,但我只想写型号

我看了一下“ltrim”和“rtrim”,但没想到这能做到

任何帮助都将不胜感激


问候。

好的,这是在VB.NET中而不是在vbscript中,但这可能会帮助您顺利完成:

Dim RegexObj As New Regex("Product\s\(FRU\)\sNumber[\s\t]+:\s(CIS.+?)$", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
ResultString = RegexObj.Match(SubjectString).Groups(1).Value

投资2个小助手功能:

Function qq(sT) : qq = """" & sT & """" : End Function

Function newRE(sP, sF)
  Set newRE = New RegExp
  newRE.Pattern = sP
  newRE.Global     = "G" = Mid(sF, 1, 1)
  newRE.IgnoreCase = "I" = Mid(sF, 2, 1)
  newRE.MultiLine  = "M" = Mid(sF, 3, 1)
End Function
和使用

  ' 3 ways to skin this cat
  Dim sInp : sInp = Join(Array( _
      "CLEI Code: COMV410ARA" _
    , "Product (FRU) Number : CISCO2821" _
    , "Part Number:73-8853-05" _
  ), vbCrLf) ' or vbLf, vbCr 
  WScript.Echo sInp
  ' (1) just search for CIS + sequence of non-spaces - risky if e.g. CLEI Code starts with CIS
  WScript.Echo 0, "=>", qq(newRE("CIS\S+", "gim").Execute(sInp)(0).Value)
  ' (2) use a capture/group (idea stolen from skyburner; just 'ported' to VBScript)
  WScript.Echo 1, "=>", qq(newRE("\(FRU\)[^:]+:\s(\S+)", "gim").Execute(sInp)(0).Value)
  WScript.Echo 2, "=>", qq(newRE("\(FRU\)[^:]+:\s(\S+)", "gim").Execute(sInp)(0).SubMatches(0))
  ' (3) generalize & use a Dictionary
  Dim dicProps : Set dicProps = CreateObject("Scripting.Dictionary")
  Dim oMT
  For Each oMT in newRe("^\s*(.+?)\s*:\s*(.+?)\s*$", "GiM").Execute(sInp)
      Dim oSM : Set oSM = oMT.SubMatches
      dicProps(oSM(0)) = oSM(1)
  Next
  Dim sName
  For Each sName In dicProps.Keys
      WScript.Echo qq(sName), "=>", qq(dicProps(sName))
  Next
要获得此输出,请执行以下操作:

CLEI Code: COMV410ARA
Product (FRU) Number : CISCO2821
Part Number:73-8853-05
0 => "CISCO2821"
1 => "(FRU) Number : CISCO2821"
2 => "CISCO2821"
"CLEI Code" => "COMV410ARA"
"Product (FRU) Number" => "CISCO2821"
"Part Number" => "73-8853-05"
我希望还有一些值得思考的东西

重要的

  • (普通)模式匹配/查找输入的某些部分
  • 捕获/分组/子匹配/圆括号从此匹配中剪切零件
  • 有时,处理问题的一般化版本会给 工作少,收获多