On Nov 2, 3:50 am, h...@risoe.dtu.dk ("Larsen, Henning Engelbrecht")
wrote:
> I want to search a string for patterns but starting the search from the
> _end_ instead of from the beginning, using a regular expression.
>
> For instance I want to find the last 'E' in the string
>
> ...looong string possibly with many E's.......E.....no capital e'
> here...3456
>
> The regular expression E$ will match an 'E' but only if it is at the
> very end. That's not the (only) match I want - also the E in '...E123'
> should match.
>
> One obvious solution is to reverse the string and use normal methods to
> find the first 'E', but there must be smarter ways, as this appears to
> be a relatively common problem to solve. Isn't it?
>

There's no way to force the regular expression engine to
shift into reverse. Even reversing the string  isn't likely to
be a win unless match(es) are near the end and the
operand is big.

One possibility though, if you're matching globally and
know the match is near the end is to set pos() to skip
over the initial postions:

pos($string) = 38;
print "found at ",pos($string) while $string =~ m/E/g;

Depending on what you're trying to do, the  'rindex'
solution that was mentioned is probably faster and
easier.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to