ruyelpequenocid
ruyelpequenocid

Reputation: 55

Regex going too far, need to limit it

I have the following data set:

C:\folder1\log.txt
C:\folder1\folder2\log.txt
C:\folder1\folder2\folder3\log.txt

I am trying to use regex to match log.txt files which may appear in the list at many levels of depth (folders, which in turn will have different names).

So assuming I want to match all the files log.txt that are "two-folders deep", I try the following regex but it is not working for me.

(.*\\){0,2}log\.txt

It is not working specifically because it matches every line, not just the one that is "two-folders deep".

How can I limit this to work as expected? What am I missing?

Upvotes: 0

Views: 145

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370859

Use a negative character set instead of ., so that you can match any character but a backslash, and make sure to anchor the pattern to the beginning of the string:

^([^\\]*\\){0,2}log\.txt

https://regex101.com/r/Q9JBcl/1

Upvotes: 1

Related Questions