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

#!/usr/bin/perl
use strict;

my (@arrayofstrings) = ('Newbie', 'NNewbie', 'NNNNNewbie', 'ewbie');
foreach my $string (@arrayofstrings) {
	print "$string -- ";
	if ($string =~ /^N?ewbie/) {
		print " match found.";
	} else {
		print " no match.";
	}
	$string =~ s/N?ewbie/*/;
	print " Transformed=$string\n";
}
exit;

Results:

Newbie --  match found. Transformed=*
NNewbie --  no match. Transformed=N*
NNNNNewbie --  no match. Transformed=NNNN*
ewbie --  match found. Transformed=*

The "?" matches 0 or 1.

Please note we had to make use of the "^" (beginning of line) operator to make this match work. Otherwise it would alway match. You can also see this through the search & replacement.



Return to main thread