[pmwiki-devel] preg dot question...

Patrick R. Michaud pmichaud at pobox.com
Fri Dec 8 08:44:24 CST 2006

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',

where strlen('$1') becomes the number of exclamation points
that were used in the markup.


More information about the pmwiki-devel mailing list