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.


In reply to Re: Pattern matching by haj
in thread Pattern matching by nursyza

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.