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!

Search for regex within a boundary

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello all,

hoping someone can help me or point me in the right direction. I have a script which I currently use but running into some problems with the output that I am getting back. I've created a new script to try to figure out the part that I am having problems which is below.

The script currently searches for a bunch of regex's then print out the name of the regex that matched. The problem that I currently have is that I want to match on the regex's between the boundary defined by the start of "*** followed by a random number/letters" and then search for the regex's within that boundary.

Or if I am going about this the wrong with my logic what would be the best way to do this?



Code:
#!/usr/bin/perl
use strict;
use warnings;

while ( <DATA> ) {
    my $boundary = $_;
    if ( $boundary =~ /stringa|stringb|stringc/gm ) {
        if ( $boundary = /stringa/g ) {
            print "Found stringa\n";
        }
        if ( $boundary = /stringb/g ) {
            print "Found stringb\n";
        }
        if ( $boundary = /stringc/g ) {
            print "Found stringc\n";
        }
    }
}

__DATA__
*** 3993.2399
stringa
stringb
stringc

*** 3993.23384j
stringc
junk data
junk data

*** 3993.23993k
stringa
stringb
junk data

Desired output
Code:
Found the following strings within boundary *** 3993.2399
Found stringa
Found stringb
Found stringc

Found the following strings within boundary *** 3993.23384j
stringc

Found the following strings within boundary *** 3993.23993k
stringa
stringb
 
To get ya started:

Code:
[kirsle@aerelon ~]$ cat test
my %boundaries = ();
my $boundary = '';
my @re = qw(stringa stringb stringc);
while (<DATA>) {
	chomp;
	if ($_ =~ /^\*{3} (.+?)$/) {
		$boundary = $1;
	}
	else {
		foreach my $re (@re) {
			if (/$re/) {
				push (@{$boundaries{$boundary}}, $re);
			}
		}
	}
}

foreach my $section (sort keys %boundaries) {
	print "Found the following strings within boundary *** $section\n";
	print join("\n", @{$boundaries{$section}});
	print "\n";
}

__DATA__
*** 3993.2399
stringa
stringb
stringc

*** 3993.23384j
stringc
junk data
junk data

*** 3993.23993k
stringa
stringb
junk data
[kirsle@aerelon ~]$ perl test 
Found the following strings within boundary *** 3993.23384j
stringc
Found the following strings within boundary *** 3993.2399
stringa
stringb
stringc
Found the following strings within boundary *** 3993.23993k
stringa
stringb

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Kirsle,

Thanks a lot for the help, that is exactly what I was looking to get back.

Cheers mate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top