PCRE:查找代码块的匹配大括号
PCRE 正则表达式有没有办法计算它遇到的字符出现次数 (n),并在找到另一个字符出现 n 次后停止搜索(特别是 {
和 } )。
这是为了获取代码块(其中可能有也可能没有嵌套代码块)。
如果更简单的话,输入将是一个单行字符串,除了大括号之外的唯一字符是数字、冒号和逗号。在尝试提取代码块之前,输入必须通过以下条件:
$regex = '%^(\\d|\\:|\\{|\\}|,)*$%';
所有大括号都将具有匹配对,并且正确嵌套。
我想知道在开始编写脚本来检查字符串中的每个字符并计算大括号的每次出现之前是否可以实现这一点。正则表达式对内存更加友好,因为这些字符串的大小可能有几千字节!
谢谢,姆尼兹。
解决方案
Is there a way for PCRE regular expressions to count how many occurrences of a character it encounters (n), and to stop searching after it has found n occurrences of another character (specifically {
and }
).
This is to grab code blocks (which may or may not have code blocks nested inside them).
If it makes it simpler, the input will be a single-line string, with the only characters other than braces are digits, colons and commas. The input must pass the following criteria before code blocks are even attempted to be extracted:
$regex = '%^(\\d|\\:|\\{|\\}|,)*$%';
All braces will have a matching pair, and nested correctly.
I would like to know if this can be achieved before I start writing a script to check every character in the string and count each occurrence of a brace. Regular expressions would be much more memory friendly as these strings can be several kilobytes in size!
Thanks, mniz.
Solution
PCRE: Lazy and Greedy at the same time (Possessive Quantifiers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
pcre 有递归模式,所以你可以做类似的
事情事情,我不认为这会比简单计数器更快或更少消耗内存,特别是在大字符串上。
这是如何在字符串中查找所有(有效)代码块
pcre has recursive patterns, so you can do something like this
the other thing, i don't think this will be faster or less memory consuming than simple counter, especially on large strings.
and this is how to find all (valid) codeblocks in a string
这正是正则表达式不擅长的地方。这是一个经典的例子。
您应该逐个字符地迭代字符串,并保留嵌套级别的计数。
This is exactly what regular expressions are not good for. It's the classic example.
You should just iterate over the string character by character, and keep a count of the nesting level.
其中: 第一行 25 表示最大出现次数。然后检查:
where: 25 on first line indicates maximum number of occurrences. then check:
这是不可能的,因为您描述的语言不是常规语言。
请改用解析器。
It is impossible since the language you are describing is not a regular language.
Use a parser instead.