BradV has asked for the wisdom of the Perl Monks concerning the following question:
I recently needed to write some code to pull a single (and first) dn out of each line in an LDAP file. Each dn is contained between '<>' and always begins with capital CN=. I used:
#!/usr/bin/perl -w my @dn; open FILE, "crap" or die $!; while (<FILE>) { chomp $_; @dn = $_ =~ /(<CN=.*?>)/; print "$dn[0]\n"; } close (FILE);
This works great. The only part I'm not sure about is the parenthesis. If I remove them, then I just get in $dn[0] the value 1 which says that yes, the regex was present. By putting the parenthesis in, I instead get the actual match. Could someone give me an explanation for that please?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Understanding this regex
by rjt (Curate) on Jun 04, 2013 at 11:58 UTC | |
|
Re: Understanding this regex
by choroba (Cardinal) on Jun 04, 2013 at 11:48 UTC | |
by BradV (Sexton) on Jun 06, 2013 at 13:37 UTC | |
|
Re: Understanding this regex
by hbm (Hermit) on Jun 04, 2013 at 13:38 UTC | |
|
Re: Understanding this regex
by Anonymous Monk on Jun 05, 2013 at 04:42 UTC | |
by Anonymous Monk on Jun 05, 2013 at 05:00 UTC | |
|
Re: Understanding this regex (perlintro)
by Anonymous Monk on Jun 04, 2013 at 23:01 UTC |