[pmwiki-devel] preg dot question...

The Editor editor at fast.st
Fri Dec 8 09:17:00 CST 2006


On 12/8/06, Patrick R. Michaud <pmichaud at pobox.com> wrote:
> On Fri, Dec 08, 2006 at 05:01:53AM -0500, The Editor wrote:
> > One other more advanced question.  In a markup like [+++ text +++], I
> > think I can get it to match, but is there are anyway to tell how many
> > plusses there are?  As in 3 as opposed to 1 or 4?  Also to make sure
> > there are the same number of +'s at beginning and end?  Or is this
> > asking too much of regex?
>
> To make sure there are an equivalent number of plusses on each side:
>
>    $x = preg_replace('/\\[(\\++)(.*?)\\1\\]/', ... )
>
> The \1 in the pattern says to match whatever was matched by the
> first set of parens.  So, if the first set of parens matches
> '+++', then the \1 has to also match '+++'.
>
> To obtain the number of characters matched, use /e and the strlen
> function in the replacement:
>
>    $x = preg_replace('/\\[(\\++)(.*?)\\1\\]/e',
>                      "Function(strlen('$1'), '$2')", ...
>
> PmWiki does this already in several places for its standard
> markup (including [+...+] and [-...-])  -- for example, the
> markup for generating a !!! heading is
>
>    Markup('^!', 'block',
>      '/^(!{1,6})\\s?(.*)$/e',
>      "'<:block,1><h'.strlen('$1').PSS('>$2</h').strlen('$1').'>'");
>
> where strlen('$1') becomes the number of exclamation points
> that were used in the markup.
>
> Pm


Ahh Pm,  this is brilliant.  Sheer brilliant.  Thanks so much.

Cheers,
Caveman



More information about the pmwiki-devel mailing list