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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with regex to modify links within a string 2

Status
Not open for further replies.

ednit

Technical User
Jan 31, 2006
16
US
I am trying to work with pre-determined links within a string, and while my regex works to replace the pre-determined links, I can't add them to an array to work with them.

Here's my code...

Code:
$mystring = qq{ Hello,
My name is red, and I am red like a barn.

[URL unfurl="true"]http://www.example.com[/URL]

This is text to test this out with.

[URL unfurl="true"]http://example.com[/URL]

These words to represent more text fill.

[[URL unfurl="true"]http://example.com?this=good&other=bad1234[/URL]]

That's it... bye... 
};

# this works
#$mystring =~ s/(\[http:\/\/)?(\.?\w+-*\w+)+\.(com|net|org|gov|edu|info)\/*[^\s]*\]/link/gi; 
#print "$mystring"; exit;

#this works
@array = $mystring =~ m/\d/g; # or \W \w, etc
print @array;

# this doesn't work
#@array = $mysting =~ m/(\[http:\/\/)?(\.?\w+-*\w+)+\.(com|net|org|info|biz)\/*[^\s]*\]/gi;
#print @array; exit;

I also tried a foreach loop with the array, and nothing is showing up.

What I want to do is take the pre-determined links (surrounded by brackets) and create redirect links using a database, so I need to work with each link rather than just replace it with pre-determined text. At least, I think this is the best approach.

I'm not receiving any errors, just a blank page. Which, the page is completely blank except for the output when it shows up.

Any help would be appreciated.
 
Well, I have part of it figured out, so I wanted to post the results here in case it's helpful.

What this regex does is identifies links within a string. I got the above code to work when I was doing a switch & replace, but I couldn't get it to work when I was trying to put the URLs into an array.

Here's the regex to put the pre-determined URLs (surrounded by brackets) into an array:

Code:
@array = $mystring =~ m/((\[http:\/\/)?(\.?\w+-*\w+)+\.([a-z]{2,4}|[a-z]{2}\.[a-z]{2,3})\/*[^\s]*\])/gi;


My new problem, however, is that I don't know how to select only the full link -- when I print out the contents of the array, I get this:

Code:
[URL unfurl="true"]http://www.example.com[/URL]
http://
.example
com
[URL unfurl="true"]http://example.com/?this=good&other=bad1234[/URL]
http://
example
com
[URL unfurl="true"]http://www.google.co.uk[/URL]
http://
.co
uk

But, I only want the full URLs, not the segments ($2, $3, $4).

Does anybody know how to do that?

Thanks.
 
Look at non-capturing parentheses on the perldoc perlre page, e.g. (?:pattern).

Annihilannic.
 
Put ?: at the beginning of your parenthesis.

i.e.

Code:
m/(?:(?:\[http:\/\/)?(?:\.?\w+-*\w+)+\.(?:[a-z]{2,4}|[a-z]{2}\.[a-z]{2,3})\/*[^\s]*\])/gi;

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thank you for your responses. The ?: pattern appears to have done it -- seems much better than the workaround I was using in the loop.

Also, thanks (again) for the example of how to use ?: -- I checked out the perldoc page, but I didn't really understand a lot of the jargon surrounding this (I'm self-taught) and wasn't sure how to implement it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top