Hello,
a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory.
So, given that the input can be a full or relative path, or a single word (including underscores and dashes, but not spaces), I'm wondering how to write a function that determines if the input is a single "word" as defined above.
For example:
- "Public_345" would classify as valid "word"
- "/home/path/to/something" would obviously be not
- "Foo bar" would also not be considered a valid "word"
As string methods are not OK, I am wondering if it would be possible to use regular expression. Initially I thought up of something like this:
match = re.compile(r"[\w-]+")
word = "abdcde_-4"
if len(re.findall(match, word)) == 1:
print "Single word"
It does feel extremely ugly, however, and I'm pretty sure it doesn't catch corner cases. Are there (much) better solutions around there?