conditional regex

I'm working on a simple templating system, don't want to use the perlmod for it, but I'm having trouble with this regex...

I want to take tags of the form
and put them as

this is the basic version:

$url = "view.cgi?page=";
$content =~ s/\[\[(.*?)\]\]/$url$1/g;

but I'd like to do relative links as well, so that


would become

(assume the current dir is in $currentdir )

in short, I want to say "if $1 doesn't contain a / then prepend it with currentdir/" in my original substitution but I don't know how...
Who is Participating?
bebonhamConnect With a Mentor Commented:
#relative path
my $currentdir="home/";

my $test="[[dir/page]] [[page2]] asdlkj asdsa/adsa [[super]] [[mak/fu]]";
$url = "view.cgi?page=";
$content =~ s/\[\[([^\]]*\/[^\]]*)\]\]/$url$1/g;
$content =~ s/\[\[([^\/]*)\]\]/$url$currentdir$1/g;

print content;                                                              

if you explain how the data in content looks, I could make it more efficient.

$content =~ s#\[\[(.*?)\]\]#$url.($1!~tr"/""&&"$currentdir/").$1#eg;
sub substitute {
    my $tmpl = shift;
    $url . ($tmpl =~ /\// ? '' : "$currentdir/") . $tmpl;

$content =~ s/\[\[(.*?)\]\]/substitute($1)/eg;
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.


Welcome back!!!

You have the following 2 questions open for a long time now. Please take some time to close these questions asap.

EE userid kisrael
Total questions asked 47 (100%)
Open questions 2

Topic Area              URL              Date              
Java Programming   11/03/00  
JavaScript   11/15/00  

Your help in closing these questions will be highly appreciated.


(NOT a moderator at EE)
maneshr, if you are an expert here it seems like a bit of a conflict of interest to moderate it as well. The idea of a moderator is that by not being a participant, they can impartially mediate a competition. Have you thought about asking EE to let you moderate in some other area?

kisrael, not sure of your exact application, but there are plenty of good templating modules at CPAN, like HTML::Template

".., if you are an expert here it seems like a bit of a conflict of interest to moderate it as well. ..."

Since i have clearly indicator that i am NOT a moderator, your point is moot.

Thanks for your comments.
I agree with leighzions, you start shouting at users for leaving two questions open. If you're clearly indicated NOT a moderator, then don't moderate.

Mr B
maneshr is trying to help,

maybe kisrael didn't know he had these questions open.

if the moderators had a problem with maneshr, they'd say.

since you two are not moderators, follow your own advice.
kisraelAuthor Commented:
Ozo and Bebonham both offered good solutions to the problem...Ozo's is more educational in someways and I only barely understand what it's about. (But it was a very direct response to how I asked the question.) Bebonham's is more rough and ready and what I should come up with myself. In the interest of people understanding my code in the future, I'm going to go with Bebonham's suggestion, so I guess Bebonham should get the points.

Thanks to you both.

And I went back, accepted an answer for a previous question, and closed the other one.  I personally didn't mind the reminder to do so, though I did get the impression that I was being asked to do so by some official representative, not by a community representative (it was because of the politeness and formality of the presentation, I didn't take the time to read the last line.)

"..And I went back, accepted an answer for a previous question, and closed the other one.  ...":

Your help in closing these questions is highly appreciated.


(NOT a moderator at EE).
maneshr, my point is not moot, you haven't addressed any of the concerns. The moderators here do a very good job. If you want to officially become one, you only need to apply. Or report bad users in the community support area.  Otherwise, there is always a question in people's mind about what rights you have to try to manage them. Your intentions are good, no one likes to see there answers ignored. I am only suggesting there are more effective ways to achieve this.


We can go on-and-on with this discussion & still get nowhere.

Therefore, in the interest of both of our times, please post a question highlighting your concerns in the "Community Support" ( topic area.

A moderator of EE will answer your question, hopefully to your satisfaction.

It doesn't really bother me that much, so I wont take it to community support. I was only trying to clarify what your role/status is here. I still don't think it is very clear so others may query it in the future. But if you are not open to ideas or suggestions, there is not much I can do about it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.