in reply to Perl pattern matching question

Use \b for word boundaries and character classes instead of alternatives of single characters:
#!/usr/bin/perl use strict; use warnings; my @strings = ('A dog ate an apple', 'A apple ate a dog', 'A apple ate a oak', ); for my $string (@strings) { $string =~ s/\b([Aa])(\s+[aeiou])/$1n$2/g; print "$string\n"; }

What would you use before the following words, though?

heir user XML yak ylem

You need pronunciation, not spelling. Update: check Text::Phonetic and related modules for that.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Perl pattern matching question
by AnomalousMonk (Archbishop) on May 06, 2015 at 18:05 UTC

    Update: Note failure in the case of 'an log'.

    c:\@Work\Perl\monks\MadhAric>perl -wMstrict -le "my $string = 'A log and a axe, an log and an axe'; print qq{'$string'}; ;; $string =~ s/\b([Aa])(\s+[aeiou])/$1n$2/g; print qq{'$string'}; " 'A log and a axe, an log and an axe' 'A log and an axe, an log and an axe'

    Even ignoring the subtleties of pronunciation you pointed out, the OPed problem statement is tricky. Some cases need 'n' deleted, so I don't see how it can be done (in a single regex) without decision-making in the replacement clause, i.e., use of the  /e modifier or equivalent trick.


    Give a man a fish:  <%-(-(-(-<