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!

Regex which includes blank line???

Status
Not open for further replies.

jaquemon

Technical User
Joined
May 22, 2007
Messages
5
Location
US
Hi everybody. You guys were great yesterday so I am back for more.
Anyway, can somebody help me out with this one. I am trying to grab multiple lines of info from something like this:

Code:
11111111.START_fhsaf
kdjfkajsdlkf

kjdkfj
skdjksjd
aaaaaaaa_END_dkjfkjd

111111111.START_fhsaf
kdjfkajsdlkf
aaaaaaaa_END_dkjfkjd

What I want to end up with is the first two paragraphs but not the third???

I have tried the following regex:
Code:
if ( /[0-9]\.START/../^$(.*)END/s)
{print OUT $_;}

But everything I try either prints everything in the input file or only the third paragraph....

Thanks in advance.
Jaquemon
 
Hi Jaquemon,

It's ok to use us as a resource, but this is really the type of thing that you could figure out yourself. I would advise you to move away from the range operator whenever you're doing something more complicated, as it's easier to assert control over the logic if you're creating the logic yourself.

Anyway, something like this would work for your purposes. It's not pretty compared to using the range operator, but it gets the job done.

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]$start[/blue] = [red]qr{[/red][purple]START[/purple][red]}[/red][red];[/red]
[black][b]my[/b][/black] [blue]$end[/blue] = [red]qr{[/red][purple]END[/purple][red]}[/red][red];[/red]

[black][b]my[/b][/black] [blue]$found[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple][blue]$start[/blue](.*?)[blue]$end[/blue][/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$1[/blue][/purple][red]"[/red][red];[/red]
		[olive][b]last[/b][/olive][red];[/red]
	[red]}[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$found[/blue][red])[/red] [red]{[/red]
		[olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple](.*?)[blue]$end[/blue][/purple][red]/[/red][red])[/red] [red]{[/red]
			[black][b]print[/b][/black] [blue]$1[/blue][red];[/red]
			[olive][b]last[/b][/olive][red];[/red]
		[red]}[/red]
		[black][b]print[/b][/black] [blue]$_[/blue][red];[/red]
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][red]/[/red][purple][blue]$start[/blue](.*)[/purple][red]/[/red][red]s[/red][red])[/red] [red]{[/red]
		[black][b]print[/b][/black] [blue]$1[/blue][red];[/red]
		[blue]$found[/blue] = [fuchsia]1[/fuchsia][red];[/red]
	[red]}[/red]
[red]}[/red]


[teal]__DATA__[/teal]
[teal]11111111.START_fhsaf[/teal]
[teal]kdjfkajsdlkf[/teal]

[teal]kjdkfj[/teal]
[teal]skdjksjd[/teal]
[teal]aaaaaaaa_END_dkjfkjd[/teal]

[teal]111111111.START_fhsaf[/teal]
[teal]kdjfkajsdlkf[/teal]
[teal]aaaaaaaa_END_dkjfkjd[/teal]

Output:
Code:
>perl scratch.pl
_fhsaf
kdjfkajsdlkf

kjdkfj
skdjksjd
aaaaaaaa_

- Miller
 
I don't like psuedo data, but here is a suggestion:

Code:
my [blue]$lines[/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/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$para[/blue][red])[/red] = [blue]$lines[/blue] =~ [red]/[/red][purple]([purple][b]\d[/b][/purple]+[purple][b]\.[/b][/purple]START.*?END)[/purple][red]/[/red][red]s[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$para[/blue][red];[/red]


[teal]__DATA__[/teal]
[teal]11111111.START_fhsaf[/teal]
[teal]kdjfkajsdlkf[/teal]

[teal]kjdkfj[/teal]
[teal]skdjksjd[/teal]
[teal]aaaaaaaa_END_dkjfkjd[/teal]

[teal]111111111.START_fhsaf[/teal]
[teal]kdjfkajsdlkf[/teal]
[teal]aaaaaaaa_END_dkjfkjd[/teal]





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top