Perl REGEX - How do extract a string in a line?

maverick-ski's Avatar, Join Date: Aug 2009
Newbie Member
Hi Guys,

In the following line:

cn=portal.090710.191533.428571000,cn=groups,dc=mp, dc=rj,dc=gov,dc=br

I need to extract this string: portal.090710.191533.428571000

As you can see this string always will be bettween "cn=" and "," strings.

Someone know one regular expression to extract this string?

Any help will be very much appreciated

Best Regards
naimish's Avatar
MyString = "1234567890"

startPos = 3
charCount = 4

Print Mid$(MyString,startPos,charCount) ' Prints "3456"
naimish's Avatar

cn=portal.090710.191533.428571000,cn=groups,dc=mp, dc=rj,dc=gov,dc=br

startPos = 4
charCount = 33

Print Mid$(MyString,startPos,charCount)
maverick-ski's Avatar, Join Date: Aug 2009
Newbie Member
Hi naimish,

I don´t understand your idea...

I have the following code:

if( /cn=/ )
$result = $_;
$result =~ s/^cn=([^,]*?),//; <-- MY REGEX EXPRESSION
return $result

in this case your REGEX is returning cn=groups,dc=mp,dc=rj,dc=gov,dc=br
I need the opposite result.

I think a REGEX where in the first time remove the string cn=
so the result string will be
portal.090710.191533.428571000,cn=groups,dc=mp,dc= rj,dc=gov,dc=br

The second REGEX will remove the entire string cn=groups,dc=mp,dc=rj,dc=gov,dc=br

To be lefting only the needed string portal.090710.191533.428571000

Can you help me?
elazab's Avatar, Join Date: Oct 2009
Newbie Member
You could try something like:

@res = ($line =~ /cn=(.*?),/ig)

print $res[0];
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Code: Perl
$line =~ /^cn=(\w+)([\d.]+)/;
print $2;

Last edited by pradeep; 27Oct2009 at 17:28.. Reason: Code block
kiruthika's Avatar, Join Date: Feb 2010
Newbie Member
my $str="cn=portal.090710.191533.428571000,cn=groups, dc=mp, dc=rj,dc=gov,dc=br";
print $1;
ansh batra's Avatar, Join Date: Jan 2010
Light Poster
$a='cn=portal.090710.191533.428571000,cn=groups,dc =mp, dc=rj,dc=gov,dc=br';
$a=~ /p[\w.]*/;