0

The goal is to output the name of devices mounted in the cluster like '\dev\spdk\nvme1n1', '\dev\spdk\nvme222n101', etc.

The first regex pattern can fulfill the requirements, but it also returns false positives like '\dev\spdk\nvme1xn1z', '\dev\spdk\nvme2xn1'. And I want to avoid the same.

The second regex is not working as expected because glob doesn't support '+' in regex searching for file/dir names?

>>> import glob
>>> 
>>> device = glob.glob(r'/dev/spdk/nvme*n*')
>>> print(device)
['/dev/spdk/nvme2n1', '/dev/spdk/nvme1n1', '/dev/spdk/nvme0n1']
>>> 
>>> device2 = glob.glob(r"/dev/spdk/nvme\d+n\d+")
>>> print(device2)
[]

Till date, the only solution I come up with is, glob.glob(r'/dev/spdk/nvme[0-9]*n[0-9]*'), but it is again capable of returning false positives like '\dev\spdk\nvme1xn1z' or '\dev\spdk\nvme11n22z'.

Is it even possible to achieve the result without having any false positives? I need a solution using regex + glob only.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sanjit
  • 57
  • 8
  • This is not regex but Unix style pattern. Check https://docs.python.org/3/library/glob.html – Benoît Zu May 17 '22 at 11:50
  • It (`\/dev\/spdk\/nvme\d+n\d+`) seems to be working for me. [Try this](https://regex101.com/r/IPu11v/1) – dhilmathy May 17 '22 at 11:50
  • @dhilmathy You are only confusing matters further. As already pointed out in an earlier comment, the argument to `glob.glob` is not a regular expression at all. (Also, there is no need to backslash regular slashes.) – tripleee May 17 '22 at 12:42
  • There is no way to solve this with glob only, though it should not be hard to apply a regex to the list returned by `glob`. – tripleee May 17 '22 at 12:44
  • @tripleee excuse my little knowledge on `glob`. – dhilmathy May 17 '22 at 13:03

0 Answers0