in reply to search for nth occurrence of a pattern in a line
Regex is better imho, since you don't know if you might want to do a pattern match some time in the future.use strict; use warnings; my ($str, $nth, $p); $str = "dsf,sdg,hjhgj,gjkh"; $p = -1; $nth = 3; while ($str =~ /,/g) { $p = pos($str) - length($&), last if !--$nth; } print substr($str, $p) if $p != -1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search for nth occurrence of a pattern in a line
by japhy (Canon) on May 17, 2006 at 20:09 UTC |