正则表达式在一行中获取 2 个特定单词
Regex to get 2 specific words on a line
我正在尝试使用正则表达式获取 2 个或更多 3 个单词。但是我很难只得到单词而不是整行。
正文如下。
3255 2010-02-14 13:17:14 WARN HA Disabling alerts for the next 600 seconds. Policy is still enforced
3256 2010-02-14 13:18:17 WARN UDM Adaptive Filter configuration disabled filter "13379: HTTP: Cool PDF Image Stream Length Buffer Overflow Vulnerability" [00000002-0002-0002-0002-000000013379]
我使用的正则表达式是
\d{4}-\d{2}-\d{2}.*disabled filter.*$ -> this work but will get the whole line.
我试图从文本中仅获取日期和禁用的过滤器,
例如。 2016-02-14 13:18:17 过滤器“13379:HTTP:Cool PDF 图像流长度缓冲区溢出漏洞”
我尝试使用管道,但它也会在不同的行上获取另一个日期
[0-9]{4}-[0-9]{2}-[0-9]{2}|disabled filter|"[0-9]{5}.+"
有人可以帮忙吗?
由于缺少语言,这里有一些解决方案。
Python
match = re.search('(?<=disabled filter ")[^"]+', subject, re.IGNORECASE | re.MULTILINE)
if match:
result = match.group()
Java
Pattern regex = Pattern.compile("(?<=disabled filter \")[^\"]+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
Notepad ++ and Sublime Text
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(?:.*)(?<=disabled filter ")([^"]+)
Group(1) 将包含 date
,而 Group(2) 包含 filter
我正在尝试使用正则表达式获取 2 个或更多 3 个单词。但是我很难只得到单词而不是整行。
正文如下。
3255 2010-02-14 13:17:14 WARN HA Disabling alerts for the next 600 seconds. Policy is still enforced
3256 2010-02-14 13:18:17 WARN UDM Adaptive Filter configuration disabled filter "13379: HTTP: Cool PDF Image Stream Length Buffer Overflow Vulnerability" [00000002-0002-0002-0002-000000013379]
我使用的正则表达式是
\d{4}-\d{2}-\d{2}.*disabled filter.*$ -> this work but will get the whole line.
我试图从文本中仅获取日期和禁用的过滤器,
例如。 2016-02-14 13:18:17 过滤器“13379:HTTP:Cool PDF 图像流长度缓冲区溢出漏洞”
我尝试使用管道,但它也会在不同的行上获取另一个日期
[0-9]{4}-[0-9]{2}-[0-9]{2}|disabled filter|"[0-9]{5}.+"
有人可以帮忙吗?
由于缺少语言,这里有一些解决方案。
Python
match = re.search('(?<=disabled filter ")[^"]+', subject, re.IGNORECASE | re.MULTILINE)
if match:
result = match.group()
Java
Pattern regex = Pattern.compile("(?<=disabled filter \")[^\"]+", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
Notepad ++ and Sublime Text
(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(?:.*)(?<=disabled filter ")([^"]+)
Group(1) 将包含 date
,而 Group(2) 包含 filter