[pmwiki-devel] Quick preg_replace question...

Patrick R. Michaud pmichaud at pobox.com
Fri Apr 27 07:21:41 CDT 2007


On Fri, Apr 27, 2007 at 12:55:04PM +0100, Hans wrote:
> Friday, April 27, 2007, 11:47:02 AM, The wrote:
> > Anyway I have it working beautifully, but need to know how to catch 0,
> > 1, or 2 spaces in a pattern?  I tried...
> >
> > '/\\n([ ]{0-2})\[([trc]{1})(.*?)\]/e'

The normal syntax for specifying "0 to 2" is {0,2} (not {0-2} as given
above).  One can also use {,2}.  So, a pattern to match 0, 1, or 2
spaces is

    / {0,2}/                    # match 0, 1, or 2 spaces

One can also use question marks here:

    / ? ?/                      # match 0, 1, or 2 spaces

> instead of ([ ]{0-2}) try  (|\s|\s\s)

Keep in mind that \s matches more than just spaces, it also matches
tabs, vertical tabs, carriage returns, and (most importantly)
newlines.  Thus, a pattern like:

    /\n(\s{0,2})/

will indeed match "\n  ", but it will also match "\n\n\n".

Hans is also correct that in /[trc]{1}/ the {1} is redundant, it's
better to simply write /[trc]/ .

> > If I can set up proper
> > security for the atttributes, I may release it as another cookbook
> > recipe...

FWIW, PmWiki provides the PQA() function, which analyzes HTML
attributes to make sure they are properly quoted (for XHTML),
and to suppress any onclick/onfocus/onblur/etc. attributes that
might be used for XSS attacks.

Pm



More information about the pmwiki-devel mailing list