Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Regular Expression help
February 23, 2006, 8:43 pm
rate this thread
links. The links can be in the following two formats.
[[xxx]] or [[yyy|xxx]]
I want to display "xxx" in all cases. This is the closest that I have
come up with
$text = ereg_replace ("\[\[([^|\]]*\|)?([^\]]*)\]\]", "",$text);
Which breaks down to this
\[\[ first brackets
([^|\]]*\|)? optional characters ending with |
([^\]]*) all characters up to ]
\]\] last brackets
Unfortunatly, it only matches single character x's or y's. In other
words it only matches "[[x]] [[y|x]]" and I dont know why because I
have the * in there to match any number of characters. Any ideas would
be very helpful.
Re: Regular Expression help
-- It searches for yyy| but makes it optional, either way it will capture
xxx into backreference 1.
You can test it here: http://tinyurl.com/jrz2u
When plugging it into a preg/ereg statement (I've only tested it with preg
though), it's good practice to double up your backslashes -- one for php,
one for the regex engine. So it would look like
$text = ereg_replace('\[(?:[^]|]+?\|)?([^]]+?)\]', '', $text);
Few of my projects recent projects--
Online Regex find/repalce utility: http://rereplace.com
Command-based online image editor: http://theprawn.com/imagiine