in reply to Pattern matching

Welcome to the monastery!

In Perl, the parentheses define what you are capturing. With (.*) you are capturing everything up to the semicolon, therefore you get everything except the leading MODULE into $1. Depending on how your input varies, you could use one of the following:

my $MODULE_NAME = 'MODULE C17 (N1, N2, N3, N6, N7, N22, N23);'; # Capture MODULE and the first "word" after MODULE if (defined($MODULE_NAME) && ($MODULE_NAME =~ /(MODULE \w+) /)) # Capture everything before the opening parenthesis, without spaces # if (defined($MODULE_NAME) && ($MODULE_NAME =~ /^(.*?)\s+\(/)) { my $module_name = $1; print "Module name = $module_name\n"; }

Note that in the second regex I had to escape the opening parenthesis to tell Perl that this is the character ( and not the start of a new capturing group.