Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex that will exclude a capture group

Tags:

regex

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"?

like image 962
bittersweetryan Avatar asked Feb 17 '12 17:02

bittersweetryan


People also ask

How do you write a non-capturing group in regex?

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)

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


1 Answers

Something like this could work for you:

\b[Gg]et(?!\w*[Dd]etails)\w+s\b
like image 85
Qtax Avatar answered Oct 24 '22 19:10

Qtax