1

One rule that I need is that if the last vowel (aeiou) of a string is before a character from the set ('t','k','s','tk'), then a : needs to be added right after the vowel.

So, in Python if I have the string "orchestras" I need a rule that will turn it into "orchestra:s"

edit: The (t, k, s, tk) would be the final character(s) in the string

1
  • 1
    While I don't have an answer, I'm curious as to the practical application of this. Commented Dec 7, 2009 at 20:48

3 Answers 3

6
re.sub(r"([aeiou])(t|k|s|tk)([^aeiou]*)$", r"\1:\2\3", "orchestras")
re.sub(r"([aeiou])(t|k|s|tk)$",            r"\1:\2",   "orchestras")

You don't say if there can be other consonants after the t/k/s/tk. The first regex allows for this as long as there aren't any more vowels, so it'll change "fist" to "fi:st" for instance. If the word must end with the t/k/s/tk then use the second regex, which will do nothing for "fist".

Sign up to request clarification or add additional context in comments.

Comments

0

If you have not figured it out yet, I recommend trying [python_root]/tools/scripts/redemo.py It is a nice testing area.

Comments

0

Another take on the replacement regex:

re.sub("(?<=[aeiou])(?=(?:t|k|s|tk)$)", ":", "orchestras")

This one does not need to replace using remembered groups.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.