#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0

    Extracting a part of the substring


    For example this is a string:

    mystring:sometext(one|two|three)

    and I need to extract only one|two|three

    I used this:
    Code:
    \(.*\)$
    And it extracts:
    Code:
    (one|two|three)
    How could I extract the string without braces,

    thanks in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    159
    Rep Power
    208
    /\((.*?)\)/

    Example:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $str='mystring:sometext(one|two|three)';
    if ($str=~/\((.*?)\)/) {
      print "found:$1\n";
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    29
    Rep Power
    0
    Thanks for your reply chorny_cpan, It worked for me, kinda...

    I got the result I was expecting to get but I feel like I'm doing something wrong here, or at least messy:

    Code:
    <?php
    $string="mystring:sometext(one|two|three)";
    preg_match("/\((.*)?\)$/",$string,$res);
    echo $res[1];
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo