First, you shouldn't use all that alternation, it's much slower (and
harder to read) than a character class. Second, you want to have a
so called zero width negative lookahead. You want to make sure
that what follows doesn't match some regular expression. Third, don't
use \1 in the replacement, use $1.
$f =~ s/\s([iaeouyE]):(?!\S)/ $1/;
Replace a whitespace, a vowel, and a colon, not followed by something
that isn't whitespace, with a space and said vowel. Alternatively, if
you know the first whitespace is always a space (or if you just want
to keep whatever whitespace it was), you could use a zero width positive
lookbehind:
$f =~ s/(?<=\s[iaeouyE]):(?!\S)//;
The (?<= ) construct is the lookbehind. There's more about
lookaheads and lookbehinds in the perlre manual page.
-- Abigail
| [reply] [d/l] [select] |
$f =~ s/\s(i|a|e|o|u|y|E):(\s|$)/ $1 /;
I also changed the \1 to a $1, since $1 will catch the matched value in parens.
Update: Of course iakobski and Hofmator are right, so don't look here, look further down.
I changed the errornous code. | [reply] [d/l] |
Good try, but you cannot use "end of string" in a character class. Also, the parser sees $] as a variable and cannot find the closing brace.
Try this one:
$f =~ s/\s(i|a|e|o|u|y|E):(\s|$)/ $1$2/;
It also captures the space or end of string and uses that rather than a space: this may or may not be what you want.
--
iakobski | [reply] [d/l] [select] |
In your solution the $ in the character class uses its special meaning and
becomes a literal '$'. You have to use a |-construct. Furthermore I would use a
characterclass for the first part ... this leads to $f =~ s/\s([iaeouyE]):(?:\s|$)/ $1 /;
Update: Oops, this was meant as a reply to busunsl not to iakobski ...
Update 2: and bikeNomad discovered some left out paras ... thanks, fixed it
-- Hofmator
| [reply] [d/l] |