Regex that excludes capture group

I am writing a regex that I need to catch lines that are plural starting with "get". For example, getContacts and getBuildings must match a regular expression. However, there are times when the text can be equal to getDetails or get**Details . I do not want the regex to include them.

I can come up with a regular expression that includes the corresponding Details group, but I want to exclude this capture group, and not include it.

 [Gg]et?\w+([Dd]etail)s 

I am not very strong at regex, but I understand my understanding of what I wrote:

a match of "g" or "G" followed by "et", and then optionally any character of the word, then the corresponding group, followed by "s".

How can I exclude results containing the word "details"?

+4
source share
2 answers

Something like this might work for you:

 \b[Gg]et(?!\w*[Dd]etails)\w+s\b 
+8
source

I believe that you are looking for the negative back side of zero width ...

http://www.regular-expressions.info/lookaround.html

 [Gg]et(?![Dd]etail)\w+s 

Assuming you want to exclude “Get Details” and “Get Info”, but accept “Get Pages” and “Get MyDetails” (NB, the end s in the original regular expression no longer include “Get Info”)

+2
source

Source: https://habr.com/ru/post/1397092/


All Articles