简体   繁体   English

正则表达式匹配一行中的所有模式

[英]Regex to match all patterns in a line

I am trying to scrape through some log files to find a line, such as this: 我正在尝试通过一些日志文件来查找一行,例如:

'A-Topeka-Firesale\:\s\*132\*\d{2,5}\*[23]\d{9}\#'

and once that line is matched, to go backward in the file and find a preceding line, like this: 并与该行匹配后,在文件中向后查找并找到前一行,如下所示:

2016-12-30 11:02:12 DEBUG[ispatcher-18269] ab.talk.this.api.Api - http://hostname:19991/trapeze?session_id=176764&manager_event=old&apostrophe=2341231231234&_operation=doc 3da48a90-0f4f-4eb3-a241-94a1f05b891b requesting:

and I need to match "http://hostname:19991/trapeze?" 并且我需要匹配"http://hostname:19991/trapeze?" , "manager_event=old" and requesting: for the second line (which is usually between 3-5 lines above the first match, to be a match. "manager_event=old"requesting:对于第二行(通常在第一场比赛之后的3-5行之间)进行比赛。

So far I have tried variations of this: 到目前为止,我已经尝试了以下方法:

for each, line in enumerate(f):
first_match = re.search(b'A-Topeka-Firesale\:\s\*132\*\d{2,5}\*[23]\d{9}\#', line)     
    if first_match:
for i in range(each, -1, -1)
    if re.match("|".join(['http://hostname:19991/trapeze', 'manager_event=old', 'requesting:']), str(f[i])):
        break

and this: 和这个:

for each, line in enumerate(f):
    first_match = re.search(b'A-Topeka-Firesale\:\s\*132\*\d{2,5}\*[23]\d{9}\#', line)     
        if first_match:
            for i in range(each, -1, -1)
                if all(re.match(regex_str, str(f[i])) for regex_str in ['http://hostname:19991/trapeze', 'manager_event=old', 'requesting: ']):
                break

And the call matches wrong lines (eg lines starting with blank spaces and with an instance of one of the matches (trapeze)). 并且该呼叫匹配错误的行(例如,以空格开头并以匹配项之一的实例(梯形)的行)。 Please what am I doing wrong and how can I do it better? 请问我在做什么错,我该怎么做呢?

Sample input: 输入样例:

 2016-01-30 00:00:27 DEBUG[-dispatcher-411] ab.talk.this.api.Api - http://hostname:19991/trapeze?manager_id=40178&manager_event=old&apostrophe=2341231231234&_operation=doc dgfgdffb-8123-4f05-ac15-7ac841afad14 requesting:
  HEADERS:
  this-is-a-header: 200*01231231234
  A-Topeka-Firesale: *132*200*01231231234#
  Host: hostname:19991
  Accept: */*
  User-Agent: AHC/2.0
  Timeout-Access: <function1>
 CONTENT:

2015-03-12 00:00:28 DEBUG[-dispatcher-747] ab.talk.this.api.Api - http://hostname:19991/trapeze?manager_id=84942&manager_event=old&apostrophe=2341231231235&_operation=ogle abcdf8237-393f-4c4b-bc46-e184cbf08d9a requesting:
  HEADERS:
  this-is-a-header: 100
  A-Topeka-Firesale: *132*100#
  Host: hostname:19991
  Accept: */*
  User-Agent: AHC/2.0
  Timeout-Access: <function1>
 CONTENT:

Very unclear what it is you really want, but after some guessing - could this be what you want? 尚不清楚您真正想要什么,但是经过一番猜测-这可能就是您想要的吗?

2016-12-30 11:02:12 DEBUG[ispatcher-18269] ab.talk.this.api.Api - http://hostname:19991/trapeze?session_id=176764&manager_event=old&apostrophe=2341231231234&_operation=doc 3da48a90-0f4f-4eb3-a241-94a1f05b891b requesting: 2016-12-30 11:02:12 DEBUG [ispatcher-18269] ab.talk.this.api.Api- http://主机名:19991 / trapeze?session_id = 176764&manager_event = old'trotrophe = 2341231231234&_operation = doc 3da48a90-0f4f-4eb3 -a241-94a1f05b891b请求:

bla bla bla bla bla bla

bla bla bla bla bla bla

bla bla bla bla bla bla

A-Topeka-Firesale: *132*12345*2123456789# A-Topeka-Firesale:* 132 * 12345 * 2123456789#

In the text above you want to match the last line. 在上面的文本中,您要匹配最后一行。 (You've only given a regex, so I made one up matching the criteria.) Finding that line will lead you to the first line, matching http://hostname:19991/trapeze? (您只提供了一个正则表达式,所以我将其与条件进行了匹配。)找到该行将使您进入第一行,匹配http://hostname:19991/trapeze? , manager_event=old and requesting: in that order, but not directly after one another. manager_event=old并发出requesting:按该顺序,但不能紧接彼此。

If I'm guessing correctly, this regex 如果我猜对了,这个正则表达式

(http://hostname:19991/trapeze.*?manager_event=old.*?requesting:).*?A-Topeka-Firesale\:\s\*132\*\d{2,5}\*[23]\d{9}\#

should (could) do it for you. 应该(可以)为你做。 It captures the first (complete) line, which is what I understand is what you're after. 它捕获了第一行(完整),这就是我所了解的。 (You haven't specified if it's something spcific you're after in it, like session_id , or whatever, but that could be "targeted" directly of course.) (您尚未指定它是否很特殊,例如session_id或其他名称,但是当然可以直接“定位”。)

Check it out here at regex101 . 在regex101处检查

Note that the e x ample uses the extended flag to allow splitting the regex up to (somewhat) improve readability, and the s ingle line flag to have . 请注意,e x ample使用扩展标志来允许将正则表达式拆分为(某种程度上)提高可读性,并使用s ingle行标志具有. match line feeds. 匹配换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM