I'm writing a regex that I need to catch strings that are plurial starting with "get". For instance getContacts
and getBuildings
should match the regex. However there are times where the text may equal getDetails or get**Details
. I do not want the regex to include these.
I can come up with a regex that includes the matching group "Details", but I want to exclude that capture group, not include it.
[Gg]et?\w+([Dd]etail)s
I'm not very strong at regex but heres my understanding of what I wrote:
match "g" or "G" followed by "et" then optionally any word character, then the matching group, followed by "s".
How can I exclude results that have the word "details"?
Sometimes, you may want to create a group but don't want to capture it in the groups of the match. To do that, you can use a non-capturing group with the following syntax: (?:X)
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
Something like this could work for you:
\b[Gg]et(?!\w*[Dd]etails)\w+s\b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With