in reply to User Code
Here's another solution that uses only one very general regexp and an external handler:
while (<>) { s/\[([^=\]\/]+)(=([^\]]+))?\]([^\[]+)\[\/\1]/&handler($1,$3,$4)/eg +; print; } sub handler { my ($tag, $value, $data) = @_; for ( $tag ) { /img/ && return "<img src='$data'>"; /link/ && return "<a href='$value'>$data</a>"; } }
What's happening here is that every time perl encounters a bracketed tag, it extracts the tag name, value (if defined) and data (the text between the tags) and calls the sub handler which returns the replacement text.
An advantage here is that if you want to add more tags, you just have to edit the handler sub rather then generating a bunch of regular expressions that your text'll have to go through serially. A disadvantage is that, well, the regular expression might be a little confusing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: User Code
by Monolith-0 (Beadle) on Dec 16, 2000 at 07:05 UTC |