Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to extract a delimited substring?

by Anonymous Monk
on May 05, 2000 at 06:06 UTC ( #10299=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on May 05, 2000 at 06:06 UTC
Q&A  > data formatting


Description:

For example, given

$string = "abe[123955785adasd]jdjajd";

I want to extract the substring between the [ and ] characters, and store it in another variable.

Answer: How to extract a delimited substring?
contributed by btrott

my $string = "abe[123955785ada]sdjdjajd"; if ( $string =~ /\[(.*?)\]/ ) { my $inside = $1; print $inside, "\n"; }

The string inside the brackets is in $inside.

The ( and ) around the .*? are "grouping parentheses"; they tell perl's regular expression engine to capture whatever it matches between them and store it in a special variable. The variable name will be a digit corresponding to the parenthesis group — $1 for the first group, $2 for the second group, etc. — counting '('s from the left.

So, in this case, perl captures what it finds between the [ and ] and stores that string in $1.

And what does it match? In this case, we've told it to match any character ("."), repeated 0 or more times ("*"), in a non-greedy manner ("?"). This last is best illustrated by example.

Say your original string was

"aslkj[2099asgjskjw]asljgn[awoeiwj]"
There are two sets of [/]-delimited strings in there. Without the "?" in the regular expression, perl would match from the first [ to the last ], putting the following into $1:

2099asgjskjw]asljgn[awoeiwj

Most likely, this isn't what you want, so we put in the non-greedy modifier to make perl do what you want.

By the way, I said that "." matches any character; this isn't strictly true. It usually doesn't match carriage returns; if you want it to match carriage returns as well, add the /s modifier at the end of the regular expression.

For more information, see perlre. It explains all of this and more.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [stonecolddevin]: o/ planetscape
    [Sinistral]: I think that just the sight of his username now causes a downvote storm. I agree, and the gratuitous use of formatting does make reading hard. I've given him +1 on things where there seemed to be actual good advice, but I think the big
    [Sinistral]: nail in coffin was the rant against a Schwarzian Transform
    [stonecolddevin]: at best, from what i've seen, his knowledge is accurate up to maybe 2002. the sheer volume of words is mostly worthy of a downvote in most cases though i think
    [erix]: ITYM Schwartzian Transform :P
    [erix]: hey planetscape :)
    [planetscape]: howdy stonecolddevin, erix
    [Sinistral]: eric Aye. I thought I typed that wrong. All hail Randall! I listen to him on FLOSS Weekly
    [Discipulus]: i think his vacuus, trombon like, absence of meaning, joined with a ostentatious style, after many years irritates the most
    [erix]: easy to get wrong -- I guess that was sund's point, too :P

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (10)
    As of 2017-06-22 20:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many monitors do you use while coding?















      Results (530 votes). Check out past polls.