Home > Software engineering >  How to create a regex in Python to detect illegal characters, including double-quotes?
How to create a regex in Python to detect illegal characters, including double-quotes?

Time:12-04

I'm creating a python script that basically creates directory structures on Windows. The names of these directories (or folders) come from user input. I want to make sure that they're not entering characters that will cause a failure when trying to create these directories.

The chars I'm trying to filter so far include: <, >, :, ", /, \, |, ?, * I think that's all of them.

But when creating the regex using the re module, it gets rather confusing because you need to compile a string:

pattern = re.compile(r'!["\/:|<>*?]')

and then when I enter something like: Foo:?

result = re.search(pattern, input_text)

The result is None. Why isn't it finding any of the the illegal chars? Admittingly, I'm new to Python and haven't used regex all that much.

CodePudding user response:

The temptation to use regex is big, but I would rather go for a non regex solution

illegal_chars = r'<>:"/\|?*'
dir_name = input("Enter dir_name: ")
for c in illegal_chars:
    if c in dir_name:
        print("ERR - illegal character '{}'".format(c))
        exit(111)
print("Name '{}' is OK for a dir".format(dir_name))

CodePudding user response:

Ok, I got this working with:

input_str = "Foo:"
pattern = re.compile(r'["\/:|<>*?]')
result = re.search(pattern, input_str)
# found ":"

This will return a match if any one of those characters are found in the string.

CodePudding user response:

invalid character for a windows directory are:

.
\\
/
:
*
?
"
<
>
|
  • Related