Search code examples
c#regexplural

Matching plurals using regex in C#


I'm looking to use regex in C# to search for terms and I want to include the plurals of those terms in the search. For example if the user wants to search for 'pipe' then I want to return results for 'pipes' as well.

So I can do this...

string s ="\\b" + term + "s*\\b";
if (Regex.IsMatch(bigtext, s) {  /* do stuff */ }

How would I modify the above to allow me to match, say, 'stresses' when the user enters 'stress' and still work for 'pipe'/'pipes'?


Solution

  • Here's a regex created to remove the plurals:

     /(?<![aei])([ie][d])(?=[^a-zA-Z])|(?<=[ertkgwmnl])s(?=[^a-zA-Z])/g
    

    (Demo & source)

    I know it's not exactly what you need, but it may help you find something out.