I'm trying to capture the position of all of the hyphens in a hyphenated word, so that I can load a hash with the position of those hyphens (in the text, not the word). At the moment, I'm trying a capture group inside of a non-capture group... but it's only capturing the last hyphen.
my $word = shift (@_);
my $word_start_pos = shift (@_);
my $text = shift (@_);
my $dash_pos = 0;
my $exp = 0;
my $pos = 0;
my $test_char = '';
if ($word =~ /^(?:[\p{L&}0-9\.\'\/]{1,}([\-])){7,}[\p{L&}0-9\.\'\/]{1,}$/) {
foreach $exp (1..$#-) {
$pos = $-[$exp];
$dash_pos = $word_start_pos + $pos;
$test_char = substr($text, $dash_pos, 1);
if ($test_char =~ /^[\-]$/) {
&load_changes('-', $dash_pos, 'Dash', ' ', 'Replace');
}
}
}
push @pos, $-[0] while /-/g;
Demo:
$ perl -Mv5.14 -ne'my @pos; push @pos, $-[0] while /-/g; say "@pos";'
abc-de-fgh
3 6
-----
0 1 2 3 4
In context, you could replace
foreach $exp (1..$#-) {
$pos = $-[$exp];
...
}
with
while ( $word =~ /-/g ) {
my $pos = $-[0];
...
}