Search code examples
regexpreg-match

Regex — only zero or one 's'


I have a name, "foo bar", and in any string, foo, foos, bar and bars should be matched.

I thought this should work like this: (foo|bar)s?. I tried some other regexes as well, but they all were like this. How can I do this?


Solution

  • (foo|bar)s? is correct...

    You should use a boundary like \b(foo|bar)s?\b. Else it would also match hihellofoos.