Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   Perl REGEX - How do extract a string in a line? (http://www.go4expert.com/forums/perl-regex-extract-string-line-t19097/)

maverick-ski 20Aug2009 00:43

Perl REGEX - How do extract a string in a line?
 
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
Pierre

naimish 20Aug2009 10:47

Re: Perl REGEX - How do extract a string in a line?
 
MyString = "1234567890"

startPos = 3
charCount = 4

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

naimish 20Aug2009 10:48

Re: Perl REGEX - How do extract a string in a line?
 
Similarly,

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 20Aug2009 23:12

Re: Perl REGEX - How do extract a string in a line?
 
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?
Regards
Pierre

elazab 10Oct2009 05:11

Re: Perl REGEX - How do extract a string in a line?
 
You could try something like:

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

print $res[0];

pradeep 27Oct2009 17:28

Re: Perl REGEX - How do extract a string in a line?
 
Code: Perl

$line =~ /^cn=(\w+)([\d.]+)/;
print $2;


kiruthika 24Feb2010 13:53

Re: Perl REGEX - How do extract a string in a line?
 
my $str="cn=portal.090710.191533.428571000,cn=groups, dc=mp, dc=rj,dc=gov,dc=br";
if($str=~/cn=(.*),cn=/)
{
print $1;
}

ansh batra 3Aug2011 16:45

Re: Perl REGEX - How do extract a string in a line?
 
$a='cn=portal.090710.191533.428571000,cn=groups,dc =mp, dc=rj,dc=gov,dc=br';
$a=~ /p[\w.]*/;
print"$&\n";


All times are GMT +5.5. The time now is 07:26.