PCRE:查找代码块的匹配括号 [英] PCRE: Find matching brace for code block

查看:131
本文介绍了PCRE:查找代码块的匹配括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PCRE正则表达式是否有一种方法可以计算遇到的一个字符(n)的出现次数,并在找到n个其他字符(具体是{})出现后停止搜索. /p>

这是为了获取代码块(它们之间可能嵌套有代码块,也可能没有).

如果它更简单,则输入将为单行字符串,除大括号外,唯一的字符为数字,冒号和逗号.在尝试提取代码块之前,输入必须通过以下条件:

$regex = '%^(\\d|\\:|\\{|\\}|,)*$%';

所有大括号都将具有匹配的一对,并且正确嵌套.

我想知道是否可以在开始编写脚本来检查字符串中的每个字符并计算括号的每次出现之前实现.正则表达式对内存更友好,因为这些字符串的大小可能为几千字节!

谢谢,mniz.

解决方案

PCRE:懒惰而贪婪同时(所有格)

解决方案

pcre具有递归模式,因此您可以执行类似的操作

$code_is_valid = preg_match('~^({ ( (?>[^{}]+) | (?1) )* })$~x', '{' . $code .'}');

另一方面,我认为这不会比简单的计数器更快或更少的内存消耗,尤其是在大字符串上.

这是查找字符串中所有(有效)代码块的方法

preg_match_all('~ { ( (?>[^{}]+) | (?R) )* } ~x', $input, $blocks);
print_r($blocks);

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)

解决方案

pcre has recursive patterns, so you can do something like this

$code_is_valid = preg_match('~^({ ( (?>[^{}]+) | (?1) )* })$~x', '{' . $code .'}');

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

preg_match_all('~ { ( (?>[^{}]+) | (?R) )* } ~x', $input, $blocks);
print_r($blocks);

这篇关于PCRE:查找代码块的匹配括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆