in reply to Match Optional Groups with 1 Required Group
G'day rem45acp,
Here's another way to do it (I've added a few more tests to ones you posted):
#!/usr/bin/env perl -l use strict; use warnings; my @tests = qw{ one one.two one.two.three four.five.six.seven one. one-two .one .one. . .. .@. one.good.egg one.-bad-.egg }; print "'$_': ", grep(! /^\w+$/, split /\./, $_, -1) ? 'bad' : 'good' f +or @tests;
Output:
'one': good 'one.two': good 'one.two.three': good 'four.five.six.seven': good 'one.': bad 'one-two': bad '.one': bad '.one.': bad '.': bad '..': bad '.@.': bad 'one.good.egg': good 'one.-bad-.egg': bad
[See split if you're unfamiliar with the 3-argument form.]
-- Ken
|
|---|