Perl regex

Hello,

I'm trying to get a quick help on regex since i'm not a regular programmer.

Below is the line i'm trying to apply my regex to..i want to use the regex in a for loop and this line will keep on changing.

subject= /C=US/ST=richardson/L=richmond/O=Corp/OU=ETG/CN=cps.corporg.com/emailAddress=matt.higgins@corporate 
.com
 
subject= /C=US/O=GeoTrust Inc./OU=Domain Validated SSL/CN=GeoTrust DV SSL CA
 
subject= /C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 Secure Server CA - G3

Currently i'm using below regex which gets me most of the Common Names (CN) from the above subject lines of the SSL certs.

$var =~ /.*CN=(.*)/

But this regex fails to catch few lines such as the 1st one.

subject= /C=US/ST=richardson/L=richmond/O=Corp/OU=ETG/CN=cps.corporg.com/emailAddress=matt.higgins@corporate 
.com

It's getting the emailAddress thing along with common name which is not required. So i tried to change the regex as below -

$var =~ /CN=(.*\/)/
 
What i get is -  'cps.corporg.net/' with this extra slash.
 

Can someone help?

So you want to capture all the elements of any dn preceded by "subject = "

my($dn)=$var=~/subject\s*=\s+\/(.+\/)/i;

EDIT: I can't read, you want to capture any common names in the dn...

my @cn= $var=~ /cn=([^/]+)\//g;

It's not working.

What i need is Only the text right after 'CN='

From above examples, the o/p should be -

1st example - cps.corporg.com
2nd example - GeoTrust DV SSL CA
3rd example - VeriSign Class 3 Secure Server CA - G3

$
$ cat f87
subject= /C=US/ST=richardson/L=richmond/O=Corp/OU=ETG/CN=cps.corporg.com/emailAddress=matt.higgins@corporate
.com
subject= /C=US/O=GeoTrust Inc./OU=Domain Validated SSL/CN=GeoTrust DV SSL CA
subject= /C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 Secure Server CA - G3
$
$ perl -lne '/CN=(.*?)(\/|$)/ and print $1' f87
cps.corporg.com
GeoTrust DV SSL CA
VeriSign Class 3 Secure Server CA - G3
$
$
1 Like

Yep. It works :slight_smile:
Thanks Tyler :slight_smile: