[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Regex for the last vowel/diphthong in a word



On Mon, Aug 02, 1999 at 04:29:58PM -0400, Michael G Schwern wrote:
> On Mon, Aug 02, 1999 at 03:51:51PM -0400, Stevie Strickland wrote:
> > Okay, I was trying to come up with a regex for the last vowel/diphthong
> > in a word that is in this set: a, o, u, au.  The best regex I've come
> > up with so far is this:
> > 
> > /([aou]u?)([^aou]*)$/
> > 
> > (Assume for the purposes of this exercise that the variable against
> >  which this match is being performed is a single word.)
> 
> Would (/([aou]|au)/g)[-1] be simplest?  Could be slow if the string is large.
> 

The idea isn't bad, but in this case it won't quite work:

The 'au' alternative will never match, because '[aou]' will match just the
'a' instead.  That's easy to fix, of course.

In the actual code that Stevie showed us, the regex is being used in a
substitution.  A list slice on /g is okay (although it doesn't generate a
list that you don't need) but it doesn't work for a substitution.


Backtracking from the end of the string might be the simplest workable
solution:

/.*(au?|[ou])/


> Maybe something with a negative lookahead... (coworker suggested this)
> 
> /([aou]|au)(?=[^aou]*$)/

I like that, it makes the replacement simpler.  It even forces 'au' to
match if it can, although it would still be good to fix the alternation.


Ronald

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe