CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Yet Another Question

 



perlkid
stranger

May 31, 2000, 8:17 AM

Post #1 of 8 (4125 views)
Yet Another Question Can't Post

 
How Can I get this

$line =~ s!<li><a href="http://.*!!;

To Do This

$line =~ s!<li><a href="http://.*!<table><li><a href="http://what was there origonaly</table>!;

Thanks,

perlkid


japhy
Enthusiast / Moderator

May 31, 2000, 8:38 AM

Post #2 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

A couple ways. You could save the .* part by using (.*), and that would put it in $1. Or you could use a "positive look-ahead".

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


s!<li><a href="(.*?)!extra stuff <li><a href="$1!g;

# or

s!(?=<li><a href=")!extra stuff !g;
</pre><HR></BLOCKQUOTE>


perlkid
stranger

May 31, 2000, 11:54 AM

Post #3 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

 
Thanks Again Japhy,

That Worked Great!! Smile Smile,

Is There Anything You Can't answer about Regex? Smile

Now I know How To Use That $1,


The Much Appreciative Perlkid


monocle
User

Jun 1, 2000, 1:35 PM

Post #4 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

perlkid, on a related note:
I believe if you use multiple (.*) in the same line you can access them with $1 and $2 and so on in the order that the (.*) appear in the line.

japhy, correct me if I'm wrong.

Plus, I have a question. What did that snippet of code do? I have seen lines like that use /// but not using !!!

Monocle


perlkid
stranger

Jun 1, 2000, 2:58 PM

Post #5 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

 
I used that code to take lines from a file and put them in a table. Since the lines had links in them they had http://www.site.com/so/on/and/on , I hate haveing to escape dosens of slashes so I used the !!! so that I wouldn't have to yet it preformed the same function. I learned to use ! from cure.

And thanks for the tip, just saves me from making future posts about .*


perlkid


japhy
Enthusiast / Moderator

Jun 1, 2000, 5:08 PM

Post #6 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

monocle - you can use characters OTHER than / for your s/// and m// and tr///.

To use $1, $2, etc., you need to parenthesize sub-patterns of your regular expressions:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


"8675309 jenny" =~ /(\d+)\s+(\w+)/;
# $1 is 8675309
# $2 is "jenny"
# but these vars are wiped out right before
# each new regular expression

($number,$name) = "8675309 jenny" =~ /(\d+)\s+(\w+)/;
# $number is 8675309
# $name is "jenny"
# and these vars last until you change them
# or they go out of scope
</pre><HR></BLOCKQUOTE>


monocle
User

Jun 2, 2000, 11:27 AM

Post #7 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

thanks japhy,

i use this piece of code to pull multiple items from a line of html and then load up the variables:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $line (@lines) {
if ($line =~ /<!--top: (.*)--><li><a href="\1\.$ext">(.*)<\/a> - <b>(.*)<\/b>\s+<i>(.*)<\/i>/) {
push(@ENTRIES,$1);
$TITLE{$1} = $2;
$LOCATION{$1} = $3;
$DATE{$1} = $4;
}
}
</pre><HR></BLOCKQUOTE>
Then I spew it all back out after sorting it and adding some extra html to support it. It seems to work quite well.


Cure
User

Jun 3, 2000, 8:16 AM

Post #8 of 8 (4125 views)
Re: Yet Another Question [In reply to] Can't Post

perldoc perlre

Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives