2

I'm dealing with a program that will have the user typing into a entry widget, then the name will be checked to see if it is valid. If it is, the file gets renamed, if not, [something else happens].

For now, I'm dealing only with the windows naming rules. I got them from this question.

My problem is with the "0-31 (ASCII control characters)". I understand that they are control characters used to deal with hardware and are not allowed in a file name.

But how am I supposed to check for them?

This website has a table of them. And I could use ord() in each char of the string to look for a value in the 0-31 range. But I'm trying to use (and learn) a bit more of regex, and would like to resolve the issue by using it.

I tried using [\x00-\x31], but it didn't work since the unicode sequence for the 0-31 chars are different.

I tried using \\[t|n|r|x]\d?\d?[a-z]? and it did work for the 0-31 numbers after I ran chr(ord(char)) on them. But this goes back to checking each char individually.

I know that I could just cast the string to os.rename() and catch the error, but I would like to avoid doing it.

I would like to use only things that are already built-in python if possible.

1
  • Note: a control character not in C0 but in ASCII is also \x7F (DEL) (with may be included with C1: \u0080-\u009F) Commented Aug 20, 2024 at 9:02

1 Answer 1

1

You can use [\u0000-\u001F] or [\x00-\x1F] to match "0-31 (ASCII control characters)"

Sign up to request clarification or add additional context in comments.

1 Comment

or [\000-\037]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.