Solved

Perl print match

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

14 Experts available now in Live!

Get 1:1 Help Now