Solved

Perl print match

Posted on 2011-03-20
5
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans

691 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