I am new to regular expressions and I am trying to implement the metaphone algorithm in vb.net. http://en.wikipedia.org/wiki/Metaphone
The algorithm has lots of steps to replace characters depending on certain conditions. (ex. replace w unless followed by a vowel). Is there a way to handle branching logic using regular expressions?
I tried
output = Regex.Replace(input, "w[^aeiou]", "")
but this removed the characters after the w too. input=wewl –> output=we (the goal is wel)
I could loop through the characters in the string to solve this problem, but I want to try to do this properly/most efficiently instead of just cranking out baggy code.
Your example could be solved by using
I can’t tell you if that’s the most efficient way, but looking at the Wikipedia page of the replacements, working like this should work.
Explanation: the round brackets define a group and the backreferences $1-$9 allow you to use the partial match in the replacement. You can get more information about backreferences on http://www.regular-expressions.info/brackets.html