Solved

conditional regex

Posted on 2002-03-13
13
276 Views
Last Modified: 2010-03-05
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
[[dir/filename]]
and put them as
view.cgi?page=dir/filename

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

[[filename]]

would become
view.cgi?page=currentdir/filename

(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...
0
Comment
Question by:kisrael
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6863012
$content =~ s#\[\[(.*?)\]\]#$url.($1!~tr"/""&&"$currentdir/").$1#eg;
0
 
LVL 8

Accepted Solution

by:
bebonham earned 200 total points
ID: 6863110
#relative path
my $currentdir="home/";

#urls
my $test="[[dir/page]] [[page2]] asdlkj asdsa/adsa [[super]] [[mak/fu]]";
$url = "view.cgi?page=";
$content=$test;
$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.

Bob
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6863214
sub substitute {
    my $tmpl = shift;
    $url . ($tmpl =~ /\// ? '' : "$currentdir/") . $tmpl;
}

$content =~ s/\[\[(.*?)\]\]/substitute($1)/eg;
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6863292
kisrael,

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   http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11751658   11/03/00  
JavaScript   http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=11880058   11/15/00  

Your help in closing these questions will be highly appreciated.

Thanks,

maneshr
(NOT a moderator at EE)
0
 

Expert Comment

by:leighzions
ID: 6863371
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
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6863632
leighzions,

".., 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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:binkzz
ID: 6864147
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
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6864697
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.
0
 

Author Comment

by:kisrael
ID: 6864839
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.)
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6865244
kisrael,

"..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.

Thanks.

maneshr
(NOT a moderator at EE).
0
 

Expert Comment

by:leighzions
ID: 6866273
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.

0
 
LVL 16

Expert Comment

by:maneshr
ID: 6866629
leighzions,

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" (http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt) topic area.

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

Thanks.
0
 

Expert Comment

by:leighzions
ID: 6866857
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
use perl to insert into MySQL database 9 121
perl rename 2 137
Perl tar error 8 32
Perl File::Find alternative 1 28
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now