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

Discussion in 'Perl' started by maverick-ski, Aug 19, 2009.

  1. maverick-ski

    maverick-ski New Member

    Joined:
    Aug 19, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    MyString = "1234567890"

    startPos = 3
    charCount = 4

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

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    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)
     
  4. maverick-ski

    maverick-ski New Member

    Joined:
    Aug 19, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  5. elazab

    elazab New Member

    Joined:
    Oct 10, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    You could try something like:

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

    print $res[0];
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    48
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    $line =~ /^cn=(\w+)([\d.]+)/;
    print $2;
     
    Last edited: Oct 27, 2009
  7. kiruthika

    kiruthika New Member

    Joined:
    Feb 24, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    my $str="cn=portal.090710.191533.428571000,cn=groups,dc=mp, dc=rj,dc=gov,dc=br";
    if($str=~/cn=(.*),cn=/)
    {
    print $1;
    }
     
  8. ansh batra

    ansh batra New Member

    Joined:
    Jan 17, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    $a='cn=portal.090710.191533.428571000,cn=groups,dc=mp, dc=rj,dc=gov,dc=br';
    $a=~ /p[\w.]*/;
    print"$&\n";
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice