Newbie dot Org HomePage
Visit one of our web buddies
Regular Expressions
Sample / Examples
Return to main thread

#!/usr/bin/perl
use strict;

my (@anyname) = ('Newbie', 'Neyoubie', 'Niwbie', 'Neabunchofcharachterswbie');
foreach my $string (@anyname) {
	print "$string -- ";
	if ($string =~ /Ne.*bie/) {
		print " match found.\n";
	} else {
		print " no match.\n";
	}
}
exit;

Results:

Newbie --  match found.
Neyoubie --  match found.
Niwbie --  no match.
Neabunchofcharachterswbie --  match found.



Return to main thread

A little extra

#!/usr/bin/perl
use strict;

my (@anyname) = ('stuff Newbie more stuff', 
	'stuff Neyoubie more stuff', 
	'stuff Niwbie more stuff', 
	'stuff Newbie otherstuff Newbie more stuff'
	);

foreach my $string (@anyname) {
	print "$string --> ";
	$string =~ s/Ne.*bie//;
	print "$string\n";
}
print "\n";

@anyname = ('stuff Newbie more stuff', 
	'stuff Neyoubie more stuff', 
	'stuff Niwbie more stuff', 
	'stuff Newbie otherstuff Newbie more stuff'
	);
foreach my $string (@anyname) {
	print "$string --> ";

	## use the non-greedy form
	$string =~ s/Ne.*?bie//;
	print "$string\n";
}

exit;

Results:

stuff Newbie more stuff --> stuff  more stuff
stuff Neyoubie more stuff --> stuff  more stuff
stuff Niwbie more stuff --> stuff Niwbie more stuff
stuff Newbie otherstuff Newbie more stuff --> stuff  more stuff

stuff Newbie more stuff --> stuff  more stuff
stuff Neyoubie more stuff --> stuff  more stuff
stuff Niwbie more stuff --> stuff Niwbie more stuff
stuff Newbie otherstuff Newbie more stuff --> stuff  otherstuff Newbie more stuff



Return to main thread
A little bit more

#!/usr/bin/perl
use strict;
my (@anyname) = ('stuff Newbie more stuff', 
	'stuff Neyoubie more stuff', 
	'stuff Niwbie more stuff', 
	'stuff Newbie otherstuff Newbie more stuff'
	);

foreach my $string (@anyname) {
	print "$string\n";
}
print "\n";

foreach my $string (@anyname) {
	$string =~ s/Ne.*?bie//;
}

foreach my $string (@anyname) {
	print "$string\n";
}

print "\n";

exit;

Results:

stuff Newbie more stuff
stuff Neyoubie more stuff
stuff Niwbie more stuff
stuff Newbie otherstuff Newbie more stuff

stuff  more stuff
stuff  more stuff
stuff Niwbie more stuff
stuff  otherstuff Newbie more stuff


The lesson to be learned here is that when using a "foreach" if you run a transform on an array element it is permanent. If you don't want the transformation to effect the array you can use a temporary variable.


Return to main thread