mess with results of a s/ inside s/ possible?

R

Robert Wallace

is it possible to "do something" with the results of s/ ?
like:
$x =~ s/(abc)/search and replace again(xyz)/;

I have this:
$_=~s/(http\:\/\/|https\:\/\/|ftp\:\/\/)(\S+)/"\<a href\=\"$1$2\"\
target=\"_blank\">$2\<\/a\>"/eg; #change http:// to links

it takes something like
check out this cool link:
http://www.cpan.org/cgi-bin/test/one/two/three/four/five.pl its great!
and makes the link a hyperlink

I want to take it a step further.
I want it to display it this way:

check out this cool link: cpan.org its great!

I want it to chop off the trailing dirs and other symbols.
I'm having trouble conceptualizing code/direction for this.

can anyone give me a clue?
thanks
 
W

Walter Roberson

:is it possible to "do something" with the results of s/ ?
:like:
:$x =~ s/(abc)/search and replace again(xyz)/;

Yes; there is always the /e modifier, which allows you to put
an arbitrary perl expression in, so you could put in a call
to a routine to compute the result.

:I have this:
:$_=~s/(http\:\/\/|https\:\/\/|ftp\:\/\/)(\S+)/"\<a href\=\"$1$2\"\
:target=\"_blank\">$2\<\/a\>"/eg; #change http:// to links

:it takes something like
:check out this cool link:
:http://www.cpan.org/cgi-bin/test/one/two/three/four/five.pl its great!
:and makes the link a hyperlink

:I want to take it a step further.
:I want it to display it this way:

:check out this cool link: cpan.org its great!

You do not need multiple levels of processing for that task.
() can nest, so you can have one part match the url and have
a part match just the essential information within that url;
use the number for the outer item where you need it for the anchor,
and use the number for the inner item where you want the human
readable part.
 
B

Ben Morrow

Robert Wallace said:
I have this:
$_=~s/(http\:\/\/|https\:\/\/|ftp\:\/\/)(\S+)/"\<a href\=\"$1$2\"\
target=\"_blank\">$2\<\/a\>"/eg; #change http:// to links

Bleech!
Use different delimiters and /x.
You *never* need to type '$_ =~'. It is assumed.
Why are you using /e? Do you know what it does?
: is not special in a regex. said:
it takes something like
check out this cool link:
http://www.cpan.org/cgi-bin/test/one/two/three/four/five.pl its great!
and makes the link a hyperlink

I want to take it a step further.
I want it to display it this way:

check out this cool link: cpan.org its great!

Howsabout (untested):

my $dom = qr/[-\w+]/; # something like that: look it up yourself :)

s< ((?:ht|f) tp s? ://) # the protocol
((?:$dom+ \.)*) # some subdomains
($dom+ \.) # the main domain name
($dom{3} | $dom+\.$dom{2}) # either a gTLD or a ccTLD with subdomain
(/ \S+) # the path section{<a href="$1$2$3$4$5" target="_blank">$3$4</a>}gx;

This will fail for ccTLDs like .de which don't use subdomains; this
can be corrected thus:

my $cc_with_sub = join '|', qw/uk us nz au/; # fill in as appropriate

s< ((?:ht|f) tp s? ://) # the protocol
((?:$dom+ \.)*?) # some subdomains
($dom+ \.) # the main domain name
($dom+\. (?:$cc_with_sub) | $dom+ ) # one or two 'boring' components
(/ \S+) # the path section{<a href="$1$2$3$4$5" target="_blank">$3$4</a>}gxo;

The *? in $2 is now necessary as otherwise the first branch of $4 will
never match.

On an unrelated topic: *don't* use target="_blank". It really pisses
me off. If I want to open the link in a new window, I can do that
myself.

Ben
 
T

Tore Aursand

[...]
it takes something like
check out this cool link:
http://www.cpan.org/cgi-bin/test/one/two/three/four/five.pl its great!
and makes the link a hyperlink

I want to take it a step further.
I want it to display it this way:

check out this cool link: cpan.org its great!

I want it to chop off the trailing dirs and other symbols. I'm having
trouble conceptualizing code/direction for this.

Check out the cool module 'URI' on the cool 'CPAN'. It will help you
accomplish what you're trying to do. No point in doing something which
someone already have done before you (and done it better, too).
 
R

Robert Wallace

Tore said:
Check out the cool module 'URI' on the cool 'CPAN'. It will help you
accomplish what you're trying to do. No point in doing something which
someone already have done before you (and done it better, too).

I wont learn anything either.
I'm learning reg.ex., or trying to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top