序列号的正则表达式

时间:2015-07-22 17:56:11

标签: regex nsregularexpression

我有这么长的有效载荷,我需要从它返回一个序列号,但它有很多序列号,我只想在某个标签后返回序列号。这是有效载荷:

USB:

USB 2.0 SuperSpeed Bus:

  Host Controller Location: Building

    Internal Card Reader:

      Product ID: 0x8406
      Serial Number: 000000000820 //i dont want this
      Building: Yes

USB 2.0 Bus:

  PCI ID: 0x8c31 


    iPhone:

      Vendor ID: 0x05ac  (skyway Inc.)
      Version: 7.02
      Serial Number: wea0aa752ada7722ac92575e98z2e89c691f4282 //i want this
      Speed: Up to 492 Mb/sec
      REC ID: 0x11100000 / 1

    skyway Internal Keyboard / Trackpad:

      Product ID: 0x0162
      Recog ID: 0x05ac  (skyway Inc.)
      Location ID: 0x14c00000 / 3
      Current Available (mA): 500
      Built-In: Yes

如何构建一个只返回以下内容的正则表达式:

wea0aa752ada7722ac92575e98z2e89c691f4282

我尝试执行以下操作但会返回所有以序列号开头的表达式:

Serial Number: [0-9]

我只想要iPhone标题后显示的序列号。

2 个答案:

答案 0 :(得分:1)

使用捕获组。

"(?s)\\biPhone:\\n\\n+(?:(?!\\n\\n).)+\\bSerial Number: (\\w+)"

DEMO

答案 1 :(得分:1)

\\biPhone:[\\s\\S]*?\\bSerial Number: (\\w+)

试试这个并抓住小组1.看演示。

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

For Group

for (NSTextCheckingResult *match in matches) {
//NSRange matchRange = [match range];
NSRange matchRange = [match rangeAtIndex:1];
NSString *matchString = [htmlString substringWithRange:matchRange];
NSLog(@"%@", matchString);
}