Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Pattern matching

by haj (Vicar)
on Nov 10, 2018 at 09:03 UTC ( [id://1225522]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1225522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-19 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found