N Sharma
N Sharma

Reputation: 34517

Regex to exclude some directories inside one directory

Hi I am reading directories inside one directory. It has names like abcModule and abcRelationModule, xyzModule

I have this regex modules/*/graphql-schema but it is considering abcModule, abcRelationModule, xyzModule.

I want to exclude abcRelationModule, which have RelationModule at the end.

Upvotes: 0

Views: 185

Answers (1)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Try this Regex:

modules\/(?![^\/]*RelationModule)[^\/]*\/graphql-schema

Click for Demo

See the JAVA code here

Explanation:

  • modules\/ - matches modules/ literally
  • (?![^\/]*RelationModule) - negative lookahead to check if the the text RelationModule is present anywhere before the occurrence of next / in the directory
  • [^\/]* - matches 0+ occurrences of any character except /
  • \/graphql-schema - matches /graphql-schema literally

If you do not want RelationModule anywhere in the directory, you can simply use ^(?!.*RelationModule).+$

Upvotes: 3

Related Questions