0

I want to match files located in multiple directories. The file could be located in various depths of a directory, for example:

/root_dir/a.txt

or /root_dir/dir_a/b.txt

or /root_dir/dir_b/dir_d/c.txt

I tried the following regular expression:

(('/root_dir')[^/.]+['.txt'])

but it seems to be incorrect.

Cheshie
  • 2,777
  • 6
  • 32
  • 51
  • 1
    Your regex matches `'`, `/`, `r`, `o`, `o`, `t`, `_`, `d`, `i`, `r`, `'` (i.e. the string `"'/root_dir'"`), followed by 1 or more characters that are not `/` or `.`, followed by a character that is either `'` or `.` or `t` or `x`. – melpomene Sep 16 '18 at 12:37
  • 1
    Why not `s.split("/")[-1]`? – Wiktor Stribiżew Sep 16 '18 at 12:37
  • Did you search for it on SO? – revo Sep 16 '18 at 12:38
  • @UnbearableLightness Thank you <3. Please write it as an answer so I can accept. – Cheshie Sep 16 '18 at 12:41
  • @revo of course I did :) – Cheshie Sep 16 '18 at 12:41
  • `re.match("/root_dir/(.*/|)[^/]+[.]txt$", line)` for each `line`. Assuming you want any `.txt` file with at least one character in the name preceding the extension. If you did not use `match` method, start with `^`. – Ondrej K. Sep 16 '18 at 12:42
  • @Cheshie Can you confirm that you are trying to match the full string and not just extract the filename? If so, you should use Wiktor's solution. Otherwise, I am happy to post my regex as an answer. – Paolo Sep 16 '18 at 12:49
  • @UnbearableLightness I'm trying to match the full string. Your answer is the one I needed. Thanks! – Cheshie Sep 16 '18 at 12:53
  • Actually not 100% what is a desired match, but above suggested `^\/root_dir[^.]+\.txt$` also matches for instance `/root_dir-do_not_match.txt`. I.e. not just files under the `/root_dir/` directory. Is that OK? But yeah, question is, do you really need to use regex? Or would treating and parsing the entries as filenames be an option? – Ondrej K. Sep 16 '18 at 12:53
  • 1
    Yeah, `^\/root_dir\/[^.]+\.txt$` is probably more accurate. – Paolo Sep 16 '18 at 12:54
  • @UnbearableLightness right, that is simpler if I actually do not care about creating groups for filename or such. My instinct made me over complicate my regex a bit. :) – Ondrej K. Sep 16 '18 at 12:56
  • This question is a common problem which asks for a string that begins with and ends to a known delimiter. I marked this question as a duplicate. If you feel the marked question doesn't provide an answer for you please edit your question accordingly. – revo Sep 16 '18 at 12:59
  • @UnbearableLightness Just a note, you are omitting all directories with a period in them. `^/root_dir/.*\.txt$` is fine. – revo Sep 16 '18 at 13:05
  • @revo Good point – Paolo Sep 16 '18 at 13:07

0 Answers0