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

how to match regex twice on the same line

Status
Not open for further replies.

perlnewbie9292

Programmer
Jul 15, 2009
25
US
Hello Perl experts,

I am not sure how/or what I need to modify to make the following lines of code match on multiple matches when they occur on the same line. Right now the regex will match correctly but only match once per line. If what I am looking for occurs twice in the same line it will only match on the first one it finds.

Code:
if ( $content =~ /((https?:\/\/)?[URL unfurl="true"]www\.\S+)/gmi)[/URL] {
    print "\n\tFound URL: $1\n";
}

If I have the following lines for example, I get the results shown below.

Code:
__data__
text with a link in between [URL unfurl="true"]http://www.kjfdjf.com/ldjfljdllfdjfjldjfljdljfljdljfdjfjdljfdljfdl/ljdljfl/jdleieir/ljldjljfd/[/URL] link in this line along with other text
[URL unfurl="true"]http://www.domain.com/test.html[/URL] line with 2 [URL unfurl="true"]www.google.com[/URL]

__results__
    Found URL: [URL unfurl="true"]http://www.kjfdjf.com/ldjfljdllfdjfjldjfljdljfljdljfdjfjdljfdljfdl/ljdljfl/jdleieir/ljldjljfd/[/URL]
    Found URL: [URL unfurl="true"]http://www.domain.com/test.html[/URL]
 
All you need to do really is turn your if into a while:

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$content[/blue] = <DATA>[red])[/red] [red]{[/red]
    [olive][b]while[/b][/olive] [red]([/red] [blue]$content[/blue] =~ [red]/[/red][purple]((https?:[purple][b]\/[/b][/purple][purple][b]\/[/b][/purple])?www[purple][b]\.[/b][/purple][purple][b]\S[/b][/purple]+)[/purple][red]/[/red][red]gmi[/red][red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][purple][b]\t[/b][/purple]Found URL: [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]text with a link in between [URL unfurl="true"]http://www.kjfdjf.com/ldjfljdllfdjfjldjfljdljfljdljfdjfjdljfdljfdl/ljdljfl/jdleieir/ljldjljfd/[/URL] link in this line along with other text[/teal]
[teal][URL unfurl="true"]http://www.domain.com/test.html[/URL] line with 2 www.google.com[/teal]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top