问题描述
我在这里的第一个问题.
my first Q here.
我有一个日志文件,其中包含多个与命中相似的字符串:
I have a log file that has multiple similar strings as hits:
Region: AR
OnlineID: Atl_Tuc
---Start---
FIFA 18 Legacy Edition
---END---
Region: FR
OnlineID: jubtrrzz
---Start---
FIFA 19
Undertale
Pro Evolution Soccer™ 2018
---END---
Region: US
OnlineID: Cu128yi
---Start---
KINGDOM HEARTS HD 1.5 +2.5 ReMIX
---END---
Region: RO
OnlineID: Se116
---Start---
Real Farm
EA SPORTS™ FIFA 20
LittleBigPlanet™ 3
---END---
Region: US
OnlineID: CAJ5Y
---Start---
Madden NFL 18: G.O.A.T. Super Bowl Edition
---END---
我想找到所有包含 fifa(fifa 作为字符串)的点击.以国际足联为例,我需要找到包含一些字符串的所有命中.
I wanna find all hits which contain fifa (fifa as a string). Fifa is example, I need to find all hits which contain some strings.
我能找到的最后一件事是这个正则表达式:(?s)(?=^\r\n)(.*?)(fifa)(.*?)(?=\r\n\r\n)
The last thing I could find is this regex: (?s)(?=^\r\n)(.*?)(fifa)(.*?)(?=\r\n\r\n)
但是当我使用它时,它会选择所有命中,包括没有 fifa 的命中,直到它在命中中找到 fifa,所以它有时会选择 1 个以上的命中 像这样.
But when I use this, it selects all hits including hits with no fifa, until it finds a fifa in a hit, so it selects more than 1 hit sometimes like this.
第二个问题是我不能在 (fifa) bcz 中使用 .*
它会导致错误的选择.
Second problem is I can't use .*
in (fifa) bcz it causes wrong selection.
我现在能做什么?
正确的输出应该是这样的:
The right output should be like this:
Region: AR
OnlineID: Atl_Tuc
---Start---
FIFA 18 Legacy Edition
---END---
Region: FR
OnlineID: jubtrrzz
---Start---
FIFA 19
Undertale
Pro Evolution Soccer™ 2018
---END---
Region: RO
OnlineID: Se116
---Start---
Real Farm
EA SPORTS™ FIFA 20
LittleBigPlanet™ 3
---END---
推荐答案
可以使用
(?si)(?:^(?<!.)|\R{2})\K(?:(?!\R{2}).)*?\bfifa\b.*?(?=\R{2}|\z)
查看正则表达式演示
详情
(?si)
-s
使.
匹配换行符字符(与.
相同)匹配换行符 ON)和不区分大小写的匹配ON(?:^(?<!.)|\R{2})
- 匹配文件的开头或两个换行符序列\K
- 省略匹配的换行符(?:(?!\R{2}).)*?
- 任何字符,出现 0 次或多次但尽可能少,不要开始双换行序列\bfifa\b
- 全字fifa
.*?
- 尽可能少的任何 0+ 个字符(?=\R{2}|\z)
- 直到双换行符或文件结尾.
(?si)
-s
makes.
match line break chara (same as.
matches newline ON) and case insensitive matching ON(?:^(?<!.)|\R{2})
- matches start of a file or two line break sequences\K
- omits the matched line breaks(?:(?!\R{2}).)*?
- any char, 0 or more occurrences but as few as possible, not starting a double line break sequence\bfifa\b
- whole wordfifa
.*?
- any 0+ chars as few as possible(?=\R{2}|\z)
- up to the double line break or end of file.
现在,如果您想将一个段落与 fifa
和 20
匹配在其某些行上,请使用
Now, if you want to match a paragraph with fifa
and then 20
on some of its line, use
(?si)(?:^(?<!.)|\R{2})\K(?:(?!\R{2}).)*?(?-s:\bfifa\b.*\b20\b).*?(?=\R{2}|\z)
(?-s:\bfifa\b.*\b20\b)
是一个修饰符组,其中 .
停止匹配换行符,它匹配一个整体单词 fifa
,然后是除换行符以外的任何 0+ 个字符,尽可能多,然后是 20
作为一个完整的单词.
The (?-s:\bfifa\b.*\b20\b)
is a modifier group where .
stops matching line breaks, and it matches a whole word fifa
, then any 0+ chars other than line break chars, as many as possible, and then a 20
as a whole word.
请参阅此正则表达式演示.
这篇关于正则表达式查找多行字符串,其中包含行之间的另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!