Update: Fixed a couple of problems as pointed out by sauoq and Random_Walk. Thanks guys :)

perlre is your friend here :)

For your first marker, you probably want to use a character class. So you might have something like: He[012].

For the middle part of your expression, it depends what you expect it to contain. If you're confident that it will only be alphanumeric characters and whitespace, then you could use ([\w\s]+)

\w denotes an alphanumeric character, \s denotes any whitespace character. These are wrapped in a character class by using the square brackets "[]", and the + quantifier is used, meaning "match one or more". The whole lot is wrapped in parentheses because you want to "capture" the string.

The end part of the expression is easy, as you said it will always end with "~~"

So, putting it all together you get (untested):

m/He[012]([\w\s]+)~~/

The captured string will be available in the $1 variable afterwards.

One point to note: If you have several such strings in a single line of data, then only the last first match will be returned. You could capture all matches into a list by using the 'g' modifier, like so:

my @strings = m/He[012]([/w/s]+)~~/g;

Hope this helps,
Darren :)


In reply to Re: Get chars between 2 markers using regular expressions by McDarren
in thread Get chars between 2 markers using regular expressions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.