简体   繁体   English

如何 Grep 在查找中搜索两次出现的字符

[英]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)*

See the test result here这里查看测试结果

Use利用

(?s)(?<=<array>)(?:(?:(?!<\/?array>)[^*])*[*]){2}.*?(?=</array>)

See proof .证明

Explanation解释

NODE节点 EXPLANATION解释
(?s) set flags for this block (with. matching \n) (case-sensitive) (with ^ and $ matching normally) (matching whitespace and # normally)为此块设置标志(与。匹配\n)(区分大小写)(与^和$正常匹配)(正常匹配空格和#)
(?<= look behind to see if there is:向后看是否有:
<array> '<array>'
) end of look-behind后视结束
(?: group, but do not capture (2 times):分组,但不捕获(2 次):
(?: group, but do not capture (0 or more times (matching the most amount possible)):组,但不捕获(0 次或多次(匹配尽可能多的数量)):
(?! 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))任何字符(0 次或多次(匹配尽可能少的数量))
(?= look ahead to see if there is:往前看是否有:
</array> '</array>'
) end of look-ahead前瞻结束

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

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