2

Basically I want to screen for the following files in python using glob.glob module:

log_fasdsaf
log_bifsd72q
log_asfd8
...

but excluding:

log_fdsaf_7832
log_fsafn_fsda
log_dsaf8_8d
...

Naively played around with linux wildcard (eg log_[!_] but apparently not working). How can I use inverse or negative wildcards when pattern matching in a unix/linux shell? seems not helping, and thanks for advices!

Community
  • 1
  • 1
Hailiang Zhang
  • 15,674
  • 21
  • 65
  • 112

2 Answers2

2

you are using the wrong character to say none of this character...

If you're looking to find any file which has log_ at the beginning, and then a load of characters where none of them are _, then you just need to do this:

log_[^_]*

will
  • 9,688
  • 6
  • 43
  • 66
1

You are close. The pattern you are looking for is log_[^_]*. It says it must have 'log_' followed by zero or more non-underscore characters.

iagreen
  • 30,052
  • 7
  • 73
  • 88
  • 1
    Actually this didn't work. The files I am looking for is "log_" following by some characters, in which there are no more underscores. The wildcard "log_[^_]*" only refers to the file names starting from "log_" and not directly followed by another "_". – Hailiang Zhang Jan 29 '13 at 21:17
  • @HailiangZhang I missed you are using `glob`. The above is regex. I think the answer is you aren't going to be able to do it with `glob`. `Glob` is very limited, specifically it does not allow you to specify a repeating pattern. I would get the results from `os.listdir` and use the regex above for filtering. – iagreen Jan 29 '13 at 21:39
  • For glob try Unix pattern: log_[!_]* – twk Feb 16 '17 at 11:53