Solved

Perl print match

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
use perl to insert into MySQL database 9 145
remove duplicates from the csv file 13 115
Transforming a Soap message to a simple xml message! 10 151
PERL - Find newest folder 12 151
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

820 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