Solved

Perl print match

Posted on 2011-03-20
5
351 Views
Last Modified: 2012-05-11

This should be simple but not getting what I want.

$line2 = '20110320222853  CN=Jones Test,OU=Users,OU=G01,OU=AAA,DC=well,DC=corp  CN=Test Jones  523999,ou=employee,o=abc,c=AN  CN=John Doe  878976,OU=abc,OU=Contacts,OU=Administrati
on,DC=well,DC=corp  cn=John Doe  878976,ou=employee,O=abc,C=AN  0  Modify  Success';

if ($line2 =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\s+(CN=.+DC=corp)\s+(CN=.*c=AN).*$/im) {
  print "$7\n";
}

From this $7 should print out:

CN=Jones Test,OU=Users,OU=G01,OU=AAA,DC=well,DC=corp

I want $7 to print out up to the space "\s+" after the first DC=corp

but I am getting everything up to the second DC=corp
such as:

CN=Jones Test,OU=Users,OU=G01,OU=AAA,DC=well,DC=corp  CN=Test Jones  523999,ou=employee,o=abc,c=AN  CN=John Doe  878976,OU=abc,OU=Contacts,OU=Administration,DC=well,DC=corp

Can someone tell me why I'm getting everything up to the second DC=corp in $7?  I would think the \s+ I have would separate that but sure is not.

Thanks,
0
Comment
Question by:bt707
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35177443
Of course

Regular expressions are greedy.  So the .+ matches for as long as it can and would still be able to match the second instance of DC=corp.
0
 

Author Comment

by:bt707
ID: 35177454
Yes that is for sure and just what I get, but need to figure out how to stop at the first DC=corp, any suggestions.
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 35177467
Try this


if ($line2 =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\s+(CN=.+?DC=corp)\s+(CN=.*c=AN).*$/im)
{
  print "$7\n";
}
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35177471
So the difference is .+?

The ? tells it not to be greedy.  Default is to go for the longest match.  This way, it would go for the shortest match.
0
 

Author Closing Comment

by:bt707
ID: 35177487
I actually already tried that figuring that's what I needed but messed up somewhere and thought it did not work.

Thanks for pointing that out, I missed is somewhere.

Thanks!!!!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now