【问题标题】:regular expression for a serial number序列号的正则表达式
【发布时间】:2015-10-12 18:25:45
【问题描述】:

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

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 标题之后的序列号。

【问题讨论】:

    标签: regex nsregularexpression


    【解决方案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);
    }
    

    【讨论】:

    • 棒极了,但无论如何可以从正则表达式本身中获取第一组。
    • @j2emanue 只有一个小组在场.. 不太了解这种语言:(
    • @vks 不需要,为什么? (?:(?!\\n\\n).)+ 断言中间没有任何空行。但是在这种情况下你失败了。所以这将匹配 iphone 旁边的段落中的字符串 Serial NUmber
    • @AvinashRaj 它根本不需要....OP 只想要 iphone 之后的序列号.....数据...你让它变得过于复杂了!!!!!!格式看起来很漂亮固定在我身上
    【解决方案2】:

    使用捕获组。

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

    DEMO

    【讨论】:

    • 注意他想要序列号“iPhone”之后
    • 我正在尝试这个,但它不起作用..如果你去rubular.com,它对你有用吗
    • 我在 rubular.com 未定义组选项中收到此错误。
    • rubular 是专门为 ruby​​ 创建的,不支持 s 修饰符。在这种情况下,您可以使用\biPhone:\n\n+(?:(?!\n\n)[\s\S])+?\bSerial Number: (\w+)
    猜你喜欢
    • 1970-01-01
    • 2011-04-28
    • 2017-08-28
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多