|
Posted by Toby A Inkster on 02/23/07 22:37
erikcw wrote:
> I'm trying to write a regex pattern to use in preg_replace. Basically
> I want to put [brackets] around every line (\n) in this variable.
> However, I need to exclude lines that already have brackets or
> quotation marks around them as well as lines that start with a
> hyphen. Lastly the lines with ** need the brackets before the **,
> instead of at the end of the line.
$x ='...'; // orig string
$x = preg_replace('/^(.*)$/m', '[$1]', $x);
$x = preg_replace('/^\[\"(.*)\"\]$/m', '"$1"', $x);
$x = preg_replace('/^\[\[(.*)\]\]$/m', '[$1]', $x);
$x = preg_replace('/^\[(.*)(\s*\*\*.*)\]$/m', '[$1]$2', $x);
$x = preg_replace('/^\[\]/m', '', $x);
That last regexp covers the behaviour of empty lines, and lines that start
with a double-asterisk.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
[Back to original message]
|