Solved

Perl print match

Posted on 2011-03-20
5
369 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

810 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