开发者

Reg-Ex for filtering out, parsing and replacing a specific string? (php)

While developing a private CMS for a client, I've had an idea to implement a php-underlying, yet server-side and flexible "language".

I'm in trouble finding a reqular-expression finding (filter..) the following string ( [..] is the code, which'll be parsed after i开发者_C百科t's been filtered out ), I want to filter the string out with the line-breaks.

<(
    [..]
)>

I was looking for a solution all night, but I didn't find a solution.


First off: Listen to Dan Grossmans advice above.

From my current understanding of your question, you want to get the verbatim content between <( and )> - no exceptions, no comment handling.

If so, try this RegExp

'/<\(((?:.|\s)*?)\)>/'

which you can use like this

preg_match_all('/<\(((?:.|\s)*?)\)>/', $yourstring, $matches)

It doesn't need case insensitivity, and it does lazy matching (so you can apply it to a string with several instances of matches).


Explanation of the RegExp: Starting with <(, ending with )> (brackets escaped of course), in between is the capturing group. At its core, we take either regular characters . or whitespace \s (which solves your problem, since line breaks are whitespace too). We don't want to capture every single character, so the inner group is non capturing - just either whitespace or character: (?:.|\s). This is repeated any number of times (including zero), but only until the first match is complete: *? for lazy 0-n. That's about it, hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜