【问题标题】:Regex match groups lookback正则表达式匹配组回顾
【发布时间】:2015-11-21 21:58:05
【问题描述】:

我试图从字符串中提取相同的数据(但在不同的“块”中多次。用例是在 Fluentd 中解析来自 Fastly 的 Syslog 消息。

我有这个日志行:

2015-08-27T12:36:58Z cache-foo1234 Name[123456]: 4.151.22.16 "-" "-" POST /api/v1/foo/61ea23fb-53fb-4364-a892-349fdf5f6dca/event?release_type=store&version=2%2E0%2E1&os=ios&device_os_version=8%2E4&device_type=iphone 304 MISS BC942858-64FA-4101-BAE1-19272490697F iPhone 5S

到目前为止,这个正则表达式(Ruby Regex):

^(?<time>[^ ]*) (?<fastly_server>[^ ]*) (?<log_name>[^ ]*) (?<host>[^ ]*) (?<na>[^ ]*) (?<na2>[^ ]*) (?<http_method>[^ ]*) (?<http_request>[^ ]*) (?<http_status>[^ ]*) (?<cache_status>[^ ]*) (?<uuid>[^ ]*) *(?<device_model>.*)$

这给了我:

  • time2015-08-27T12:36:58Z
  • fastly_servercache-foo1234
  • log_name姓名[123456]:
  • host4.151.22.16
  • http_method发帖
  • http_request /api/v1/foo/61ea23fb-53fb-4364-a892-349fdf5f6dca/event?release_type=store&version=2%2E0%2E1&os=ios&device_os_version=8%2E4&device_type=iphone
  • http_status304
  • cache_status小姐
  • uuidBC942858-64FA-4101-BAE1-19272490697F
  • device_modeliPhone 5S

这是完美的,但我怎样才能回去提取即。 61ea23fb-53fb-4364-a892-349fdf5f6dcaeventdevice_os_version 的值来自具有相同正则表达式的相同字符串?

【问题讨论】:

    标签: ruby regex regex-lookarounds fluentd fastly


    【解决方案1】:

    这是更新的正则表达式:

    ^(?<time>[^ ]*) (?<fastly_server>[^ ]*) (?<log_name>[^ ]*) (?<host>[^ ]*) (?<na>[^ ]*) (?<na2>[^ ]*) (?<http_method>[^ ]*) (?<http_request>\/api\/v\d+\/foo\/(?<guid>[^\/]+)\/(?<event>[^\/?]+)[^ ]*device_os_version=(?<devosver>[^=&]+)[^ ]*) (?<http_status>[^ ]*) (?<cache_status>[^ ]*) (?<uuid>[^ ]*) *(?<device_model>.*)$
    

    demo

    我刚刚将您的 (?&lt;http_request&gt;) 部分替换为

    (?<http_request>\/api\/v\d+\/foo\/(?<guid>[^\/]+)\/(?<event>[^\/?]+)[^ ]*device_os_version=(?<devosver>[^=&]+)[^ ]*)
                    ^----------------^^-----GUID-------^^--event-------^     ^-------OS Version--------   -------^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-27
      • 2023-02-22
      • 2022-01-14
      • 2018-01-02
      • 2012-02-27
      • 1970-01-01
      • 2022-06-18
      • 2021-03-21
      相关资源
      最近更新 更多