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

#!/usr/bin/perl
use strict;

my (@gerbal) = (
		'Newbie',
		'Notbie', 
		'Oldie', 
	);

foreach my $foot (@gerbal) {
	print "$foot -- ";
	if ($foot =~ /Newbie|Oldie/) {
		print " match found.\n";
	} else {
		print " no match.\n";
	}
}
exit;

Results:

Newbie --  match found.
Notbie --  no match.
Oldie --  match found.


Return to main thread

A little extra

#!/usr/bin/perl
use strict;

my (@gerbal) = (
		'Newbie',
		'Notbie', 
		'Nutbie', 
		'Oldie', 
	);

foreach my $foot (@gerbal) {
	print "$foot -- ";
	if ($foot =~ /N(?:ew|ot)bie/) {
		print " match found.\n";
	} else {
		print " no match.\n";
	}
}
exit;

Results:

Newbie --  match found.
Notbie --  match found.
Nutbie --  no match.
Oldie --  no match.

This fun form contains a couple of mysteries.
Why use "(?:ew|ot)"?
Why not just use "(ew|ot)"?
Both would work. But they have different side-effects.
We'll leave the difference for you to discover later as you develop your Perl.


Return to main thread