[英]How to Grep Search two occurrences of a character in a lookbetween
I seem to have to perpetually relearn Regex & Grep syntax every time I need something advanced.每次我需要一些高级的东西时,我似乎都必须永远重新学习 Regex 和 Grep 语法。 This time, even with BBEDIT's pattern playground, I can't work this one out.
这一次,即使使用 BBEDIT 的模式游乐场,我也无法解决这个问题。
I need to do a multi-line search for the occurrence of two literal asterisks anywhere in the text between a pair of tags in a plist/XML file.我需要对 plist/XML 文件中一对标签之间的文本中任何地方出现的两个文字星号进行多行搜索。
I can successfully construct a lookbetween so:我可以成功地构建一个lookbetween,所以:
(?s)(?<=<array>).*?(?=</array>)
I try to limit that to only match occurrences in which two asterisks appear between tags:我尝试将其限制为仅匹配标签之间出现两个星号的情况:
(?s)(?<=<array>).*?[*]{2}.*?(?=</array>)
(?s)(?<=<array>).+[*]{2}.+(?=</array>)
(?s)(?<=<array>).+?[*]{2}.+?(?=</array>)
But they find nought.但他们一无所获。 And when I remove the {2} I realize I'm not even constructing it right to find occurrences of one asterisk.
当我删除 {2} 时,我意识到我什至没有正确构建它来查找一个星号的出现。 I tried escaping the character /* and [/*] but to no avail.
我试过 escaping 字符 /* 和 [/*] 但无济于事。
How can i match any occurrence of blah blah * blah blah * blah blah?我如何匹配任何出现的 blah blah * blah blah * blah blah?
[*]{2}
means the two asterisks must be consecutive. [*]{2}
表示两个星号必须是连续的。
(.*[*]){2}
is what you're looking for - it contains two asterisks, with anything in between them. (.*[*]){2}
是你要找的 - 它包含两个星号,它们之间有任何东西。
But we also need to make sure the regex is only testing one tag closure at the same time, so instead of .*
, we need to use ((?.<\/array>).)*
to make sure it won't consume the end tag </array>
while matching .*
但是我们还需要确保正则表达式同时只测试一个标签闭包,所以我们需要使用
((?.<\/array>).)*
代替.*
* 来确保它不会在匹配.*
时使用结束标记</array>
The regex can be written as:正则表达式可以写成:
(?s)(?<=<array>)(?:((?!<\/array>).)*?[*]){2}(?1)*
Use利用
(?s)(?<=<array>)(?:(?:(?!<\/?array>)[^*])*[*]){2}.*?(?=</array>)
Explanation解释
NODE![]() |
EXPLANATION![]() |
---|---|
(?s) |
set flags for this block (with. matching \n) (case-sensitive) (with ^ and $ matching normally) (matching whitespace and # normally)![]() |
(?<= |
look behind to see if there is:![]() |
<array> |
'<array>' |
) |
end of look-behind![]() |
(?: |
group, but do not capture (2 times):![]() |
(?: |
group, but do not capture (0 or more times (matching the most amount possible)):![]() |
(?! |
look ahead to see if there is not:![]() |
</?array> |
</array> or <array> ![]() </array> 或<array> |
) |
end of look-ahead![]() |
[^*] |
any character except: '*'![]() |
)* |
end of grouping![]() |
[*] |
any character of: '*'![]() |
){2} |
end of grouping![]() |
.*? |
any character (0 or more times (matching the least amount possible))![]() |
(?= |
look ahead to see if there is:![]() |
</array> |
'</array>' |
) |
end of look-ahead![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.