home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Perl RegExp

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1518
Discussions: 200,952, Posts: 2,379,478, Members: 246,321
Old October 19th, 2002, 06:37 PM   Digg it!   #1 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
Perl RegExp

OK I have a VERY simple script that's not playing fair lol
Code:
#! /usr/bin/perl -w

my $rootdir = "/home/vass/Stuff";
my $imgtag = "<img src=";
opendir(DNAME,$rootdir) || die "Unable to open dir";
open(HTML, ">$rootdir/pics.html") || die "damnit, can't open file $!";

print HTML "<html>";
print HTML "<head>";

while($filename = readdir(DNAME))
{
    $filename =~ s/ /%20/;
    print HTML "$imgtag$filename> \n";
}

closedir(DNAME);

print HTML "</head>";
print HTML "</html>";
I'm pretty sure you can figure out what it does, but I'm just learning perl
My problem is I thought the regular expression would remove ALL instances of (in this case) the space and replace it with the %20!?!
It only seems to replace 1
vass0922 is offline   Reply With Quote
Old October 19th, 2002, 07:14 PM     #2 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Alberta, Canada
Posts: 563
I've only done very little with regular expressions, but do you need an asterisk '*' in there??
Quote:
You can follow any character, wildcard, or series of characters and/or wildcard with a repetiton. Here's where you start getting some power:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times

link

so you'd end up with (something like this I think!!):

$filename =~ s/* /%20/; or is it this: $filename =~ s/ */%20/;

also, just above that quote from that page there's this: "\s Match whitespace character"

so could you do something like this(??):
$filename =~ s/*\s/%20/; or would it be this: $filename =~ s/\s*/%20/;

just remember what I said at the start, me not so good with regular expressions, so I could be waaay off...

g'luck!!

hhmmm, might have to start playin with these little goodies sometime(regular expressions)... don't have perl installed, but I have php and I believe it has 'em...

PS: woohoo!! # 501!!!!
^hyd^ is offline   Reply With Quote
Old October 20th, 2002, 03:55 AM     #3 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
ok we're getting closer lol

Code:
while($filename = readdir(DNAME))
{
    $filename =~ tr/ /%20/;
    print HTML "$imgtag$filename> \n";
}
OK
tr works better than s for this one lol
HOWEVER... I can't get it replace the text with %20.. instead it replaces it with %
I've tried it with star, I've tried with
$filename =~ tr/\s/%20/; -- doesn't like that at all
$filename =~ tr/[ ]/[%20]/; -- same results
$filename =~ tr/ */%20/; -- same results

still no luck
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
vass0922 is offline   Reply With Quote
Old October 20th, 2002, 11:13 AM     #4 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
It might be trying to interpret the %. Maybe through a backslash in front of it.

tr/ /\%20/
tenor_david is offline   Reply With Quote
Old October 20th, 2002, 05:36 PM     #5 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
I tried that... same results
vass0922 is offline   Reply With Quote
Old October 20th, 2002, 06:48 PM     #6 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Alberta, Canada
Posts: 563
well, I got perl installed for iis. Set up a test suite, and it worked! ONCE, and only once...

now, as soon as it hits the "opendir()" statement, it just stops processing... bah... have to get back to it later, need a shower, and some food!

ciao!
^hyd^ is offline   Reply With Quote
Old October 20th, 2002, 07:26 PM     #7 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
lol
you get active perl?
hmmm lemme see if that'll work on this winbox

-- edit --
ok ya it has same prob over here
with s/ /%20 it does replace it with %20 but it only replaces one

with tr/ /%20 it replaces all of them.. with %

I realize that tr/ / replaces on a per character basis so I think its only replacing the first char it finds in the /%20
Now how to get it to either replace all with s/ / or replace with all the characters with tr/ / LOL

ahhhh and these are the days of our lives

Last edited by vass0922 : October 20th, 2002 at 07:32 PM.
vass0922 is offline   Reply With Quote
Old October 20th, 2002, 07:39 PM     #8 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
AHH HA!! think I got it

Code:
#! /usr/bin/perl -w

my $rootdir = "C:\\";
my $imgtag = "<img src=";
my $replace = "%20";
opendir(DNAME,$rootdir) || die "Unable to open dir";
open(HTML, ">$rootdir/pics.html") || die "damnit, can't open file $!";

print HTML "<html>";
print HTML "<head>";

while($filename = readdir(DNAME))
{
    $filename =~ s/ /$replace/g;
    print HTML "$imgtag$filename> \n";
}

closedir(DNAME);

print HTML "</head>";
print HTML "</html>";
I needed the $filename =~ s/ /$replace/g; for global replace at the end of the line
its the little things that get ya

Thanks for the help

hmm now to replace the & ampersand .. happen to know the char for that? lol
I'll start looking...
vass0922 is offline   Reply With Quote
Old October 20th, 2002, 07:41 PM     #9 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Alberta, Canada
Posts: 563
well, actually, I think it did replace all spaces with the full %20, but when I deleted the contents from pics.html and tried it again it just wouldn't do it!! only the once, then it just stopped!! no error messages or anything... can't even get a simple "print" statement to work after the opendir() command...

back to have another look now tho!! hehehe
^hyd^ is offline   Reply With Quote
Old October 20th, 2002, 07:45 PM     #10 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,386
ok found some
http://nssdc.gsfc.nasa.gov/nssdc/for...Characters.htm

gotta love nasa lol
$filename =~ s/ /%20/g;
$filename =~ s/&/%26/g;
works good so far, will have to give it a shot on the linux box tonight
vass0922 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2943)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
Regular Build (11)
windows 7 problem (7)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (6)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (10)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Recent Discussions
tv not turn on-makes clicking sound (2)
SELLING FRESH,CVV,LOGINS,DUMPS,TRACKS.. (0)
CPU fan stops spinning randomly (9)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Modern Warfare 2: Who Bought It? (63)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Laptop with wireless problem. (3)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
windows vista security holes (10)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)


All times are GMT -4. The time now is 04:55 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28