English 中文(简体)
PCRE: Find matching brace for code block
原标题:

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);
问题回答

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.

$regex= %^(\d|\:|\{|\}|,){0,25)$% ;
preg_match($regex,$target,$matches);

where: 25 on first line indicates maximum number of occurrences. then check:

$n=count($matches);

It is impossible since the language you are describing is not a regular language.

Use a parser instead.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签