Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression

Status
Not open for further replies.

teroy

Programmer
Oct 17, 2000
67
AU
Hi Guys,
I have the following string

my $test=" check out this link it could be
and a regex which follows:
$test=~s/[^\/] http:\/\/
what i want to happen..is if it finds want it to add http:// in front of it.
But..if http:// is already in front of don't do anything.

My above regex doesn't quite do that exactly..anyone have any ideas

TIA

troy
 
Code:
#!/usr/local/bin/perl
# a little confusion in your attempt.... a little 
# explaination of your problems might be constructive.
#
#                a word boundry
#                   |
#$test=~s/[^\/][URL unfurl="true"]www*\b/[/URL] http:\/\/[URL unfurl="true"]www$1/gi;[/URL]
#          |      |                 |
#          |      |                must point back to 
#          |   any number          something in the pattern
#          |   of w's ?            in paren's.  You have no
#          |   you don't need      paren's in the pattern,
#          |   the '*'           therefore $1 is empty here.
#          |                        
#        In a character class [],
#        the up carat '^' negates
#        the class.  So, [^\/] 
#        matches anything other than
#        a '/'.  
#        
# Maybe this is what you are looking for....

$test = '[URL unfurl="true"]http://www.some_server.com';[/URL]
$test =~ s/(http:\/\/)*([URL unfurl="true"]www.*)/http:\/\/$2/is;[/URL]
#          (         )*    |             |
#           \         /    |           insert contents of
#            match zero    |           second set of
#            or more of    |           paren's from match
#            http://       |
#                       catch '[URL unfurl="true"]www followed[/URL] by anything'
#                       in $2 to put back with http://
#
# If this finds http:// on the front of the string
# it discards it.  Thus, all strings need http:// inserted
# into the front of the string.

print "$test\n";

HTH





If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Thankyou Goaboating...sorry my explanation was a bit incomprehensible.

Your solution nearly worked for me example

basically the url could be anywhere in the string..and it might contain http:// or it might just contain
my $test=" check out this link it could be
Your reg ex:
$test =~ s/(http:\/\/)*(
When i printed out $test i got the following:
check out this link it could be
I tried this....
$test =~ s#(?<!
Thanks for your help

-troy :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top