I have a lot of fields to validate with different regex
so I have a Funktion
# check for valid chars
def valid_chars(label,value, expression):
res = bool(re.match(expression, value)) # check valid
if not res:
show_err(f"invalid umlaut or char in {label}")
return 0 # return True of no other char
else:
return 1
and call it with
error += valid_chars("CA",input,"'[a-zA-Z0-9-\s]+$'")
but in debug I see python escapes me the backslash what breaks me the regex
‘[a-zA-Z0-9-\s]+$’")
is there any workaroud, could not find some meaningfull at google
If I understand you right I shout pass “‘[a-zA-Z0-9-’\s’]+'")
but stiil i get a second backslash "'[a-zA-Z0-9-'\\s']+'” and so I get invalid back even if string is valid
Could you please be more specific with “function fails”? Are you getting an error? A warning? Is it not parsing the way you expect? If not, which cases don’t work as you want?
def check_entries():
show_err(" ") # clear error message
# CA_NAME allowed characters "a-z, A_Z, 0-9, hyphen"
error = 0
input = CA_var.get() # get input
error += is_empty("CA",input)
if error == 0: # if empty following checks will fail
# error += valid_chars("CA",input,"'[a-zA-Z0-9-\s]+$'") # check valid
error += hyphen_pos("CA",input) # check hyphen pos
# like here meand like the next line the regesx works like expected
res = bool(re.match('[a-zA-Z0-9-\s]+$',input)) # check valid
if not res:
error += 0
if error > 0: # at least one check failed
return # return here
else: # validation OK
CA_var.set(input) # rewrite input
global CA
CA = input # save input
But when I put that in a function to save 2 lines (i have lot of fields with different regex) with this function
# check for valid chars
def valid_chars(label,value, expression):
res = bool(re.match(expression, value)) # check valid
if not res:
show_err(f"invalid umlaut or char in {label}")
return 0 # return True of no other char
else:
return 1
I get the backslash doubled and I get 1 what means fail in any case if the input string is ok or no
Can you please provide a small, complete, isolated example? I.e. a python script I can just copy and execute to see the problem you are having with the regex?
I don’t know what your code is supposed to do and I don’t know the architecture you are trying to accomplish. And if you think the issue is actually with the regex, please don’t try to explain it here - it’s just going to be a distraction from the problem.
Let’s look at the regexes you’ve tried and what they mean:
"'[a-zA-Z0-9-\s]+$'"
This means:
' Single quote
[ Start of character set
a-z Letters in the range 'a'-'z'
A-Z Letters in the range 'A'-'Z'
0-9 Digits in the range '0'-'9'
- Hyphen
\s Whitespace
] End of character set
+ Match the character set one or more times
$ End of line
' Single quote
Saying that the end of the string must also be a single quote will always fail because it’s impossible for it to be both.
Also, you should either use a raw string or double the backslash.
"r'[a-zA-Z0-9-\s]+$'"
This means:
r' Letter 'r' then a single quote
[ Start of character set
a-z Letters in the range 'a'-'z'
A-Z Letters in the range 'A'-'Z'
0-9 Digits in the range '0'-'9'
- Hyphen
\s Whitespace
] End of character set
+ Match the character set one or more times
$ End of line
' Single quote
Again, this is saying that the end of the string must also be a single quote.
"'[a-zA-Z0-9-'r'\s'']+$'"
This means:
' Single quote
[ Start of character set
a-z Letters in the range 'a'-'z'
A-Z Letters in the range 'A'-'Z'
0-9 Digits in the range '0'-'9'
- Hyphen
' Single quote
r Letter 'r'
' Single quote (again)
\s Whitespace
' Single quote (third time)
' Single quote (fourth time)
] End of character set
+ Match the character set one or more times
$ End of line
' Single quote
Yet again, this is saying that the end of the string must also be a single quote.
If you want it to be one or more of ‘a’-z’, ‘A’-Z’, ‘0’-‘9’, hyphen, or whitespace, then the regex is:
And where is the differene to my regex, it is exact what I want in this cases the is a dot or space allowed.
As I wrote that works fine as long I do not try to pass it as a parameter. Even with the r in front it doubles me the backslash. i am using PyCharm as IDE an if trace the program in debug mode, where I can see the incoming parameters I see the \
thank you, but it is fixed. In the intention to pas the regex as a string i quoted the expresion, which wa a string, again, so I got double-quote quote string quote double-quote, when I left the double-quotes everything runs fine. Have a nice sunday