I found an interesting singularization class that works pretty okay, but I ran into an issue and as I’m not very clued up with regular expressions can’t seem to figure out how to fix this.
Given the following:
private static readonly IDictionary<string, string> Singularizations =
new Dictionary<string, string>
{
{"people", "person"},
{"oxen", "ox"},
{"children", "child"},
{"feet", "foot"},
{"teeth", "tooth"},
{"geese", "goose"},
{"(.*)ives?", "$1ife"},
{"(.*)ves?", "$1f"},
{"(.*)men$", "$1man"},
{"(.+[aeiou])ys$", "$1y"},
{"(.+[^aeiou])ies$", "$1y"},
{"(.+)zes$", "$1"},
{"([m|l])ice$", "$1ouse"},
{"matrices", @"matrix"},
{"indices", @"index"},
{"(.+[^aeiou])ices$","$1ice"},
{"(.*)ices", @"$1ex"},
{"(octop|vir)i$", "$1us"},
{"(.+(s|x|sh|ch))es$", @"$1"},
{"(.+)s", @"$1"}
};
If I test against the word “Levels”, it returns “Lefls”, basically failing to match the word on: {"(.*)ves?", "$1f"}
Is there any way that I can add a new rule to this for levels? I’ve tried adding {“(.*)els?”, “$1f”} but the regular expression validator just skips over this.
Updated Note:
Thanks for all the feedback, I had a look at the new PluralizationService but this is specific to .Net 4.0 and my client will not be upgrading from 3.5 any time soon so had to find another way around this.
I think the issue is that (.*)ves? should have a $ at the end, so it’s not matching the
levels part of levels.