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!

Extracting multiple matches 1

Status
Not open for further replies.

wxb2744

Technical User
Jan 23, 2008
9
0
0
EU
I am trying to extract information from a text file and am having problems extracting the matches.

The sample file is: -

zone: zone_name
50:06:0e:80:04:58:1d:04
10:00:00:00:c9:6a:11:02

and the regular expression is: -

( $file =~ /zone:\s+(\w*)(?:\n\s*((?:[a-f\d]{2}:){7}[a-f\d]{2})){2}/ )

This matches but gives me a list of two items, the first match and the last, but it should be giving me three since the second part of the expression is matching exactly twice.

I assume that somewhere along the line the middle match is being clobbered, but I don't know how to stop this.

Thanks for any assistance.

Warren
 
Try using while loop
Code:
while ( $file =~ /zone:\s+(\w*)(?:\n\s*((?:[a-f\d]{2}:){7}[a-f\d]{2})){2}/ )
{
print $1." ".$2 ;
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
As you might have figured out by now, you can't use enclosed captured parenthesis in that way. Any particular paren will only capture the last match, not a list of matches.

To do that you must explicitly list out a paren for each value that you want to capture. Or alternatively, you could use a while loop like spookie attempted to demonstrate.

The following will show you how to use both alternative methods:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$data[/blue] = [url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$/[/blue][red];[/red] <DATA>[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]Original:[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue] =~ [red]/[/red][purple]zone:[purple][b]\s[/b][/purple]+([purple][b]\w[/b][/purple]*)(?:[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*((?:[a-f[purple][b]\d[/b][/purple]]{2}:){7}[a-f[purple][b]\d[/b][/purple]]{2})){2}[/purple][red]/[/red][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]1 - [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]2 - [blue]$2[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]3 - [blue]$3[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]my[/b][/black] [blue]$mac_addy[/blue] = [red]qr{[/red][purple](?:[a-f[purple][b]\d[/b][/purple]]{2}(?::[a-f[purple][b]\d[/b][/purple]]{2}){7})[/purple][red]}[/red][red]i[/red][red];[/red]

[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple]Non-grouped:[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue] =~ [red]/[/red][purple]zone:[purple][b]\s[/b][/purple]+([purple][b]\w[/b][/purple]*)[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*([blue]$mac_addy[/blue])[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*([blue]$mac_addy[/blue])[/purple][red]/[/red][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]1 - [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]2 - [blue]$2[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]3 - [blue]$3[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple]while:[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]if[/b][/olive] [red]([/red][blue]$data[/blue] =~ [red]m{[/red][purple]zone:[purple][b]\s[/b][/purple]+([purple][b]\w[/b][/purple]*)[/purple][red]}[/red][red]gcms[/red][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]zone - [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[olive][b]while[/b][/olive] [red]([/red][blue]$data[/blue] =~ [red]m{[/red][purple][purple][b]\G[/b][/purple][purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*([blue]$mac_addy[/blue])[/purple][red]}[/red][red]gcms[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]Addy - [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]

[teal]zone: zone_name[/teal]
[teal]          50:06:0e:80:04:58:1d:04[/teal]
[teal]          10:00:00:00:c9:6a:11:02[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top