--- Nathaniel Mallet <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to retrieve a substring from a string, but I'm not
> sure exactly where and how big that substring is. The substring is
> delimited by a start and end special character. It was suggested to
> me to write two regular expression, one that would match everything
> up to and including the start special character, and one to match
> everything following and including the end special character, and
> replacing the matches with nothing.
>
> I thought finding the location of the start and end special
> characters, then calling substr().
>
> And so I have to questions:
> 1 - How would I find the index for a particular character in a
> string?
> 2 - What's the best method, everything considered (simplicity,
> performance, etc).
index() will give you the start point, but substr() wants a length, so
getting the index() of the end would then require a subtraction to get
length. Not horrible, but more code makes a statement harder to read.
I'd just do a match:
my $SC = $startcharacter;
my $EC = $endcharacter;
my($substr) = $fullstring =~ /$SC(.*?)$EC/;
Note that the parens around the $substr target variable are required to
give it a list context, so that the value is returned from the match.
Test it -- I'm not awake good yet this morning. =o)
__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year! http://personal.mail.yahoo.com/