0

[Solved] Why did my regex not work for "-" in the earlier version of this Python code ?

Hi. I just realized one of my bits failed to throw an exception in accordance with my regex. I later changed the concerning part of the if statement to have the first predicate, an inequality, deal with it. Initially it was "if n >= 256 or r".*?\D" in str(n):" With this one negative inputs with the minus sign weren't dealt with by the regex but at the end due to the print statement conditional. So what's wrong with the regular expression, why does it work when other symbols are used but not with the minus sign? Edit: I gave it one last try, and wrote or re.match(r"^.*?\D?", str(n)) == True (also tried without the boolean assignment) didn't work. Started mixing things up :(. Going to bed now, I will be checking the replies later. Thanks in advance. https://code.sololearn.com/c6Jo1WdlWjkV/?ref=app

26th May 2022, 2:32 AM
Korkunç el Gato
Korkunç el Gato - avatar
4 Antworten
+ 5
Korkunç the Terrible Frusciante , i think that the validation of the rgb input can be done without any regex. it just needs to use .isdigit() and to check if the input value is in the correct range. in the file you can find some comments and a demonstration how it can be applied. an other possible way could be to use try... except... https://code.sololearn.com/c9D72JS4oBqn/?ref=app
26th May 2022, 9:01 AM
Lothar
Lothar - avatar
+ 3
Korkunç the Terrible Frusciante , things i have done: i store all the trials in py files on local device, using a name for the purpose of the code, also mention functions, methods like: -> count all alphanum characters using - for loop - filter - comprehension - regex.py i also use comments in the code to describe things that are special or new to me so when i am searching with my file manager i can use keywords to search for. it is also possible to search inside the files for a match. when things are new i use google to find: 'python find duplicated characters'. mostly you will get a good choice of matches. have a look at them. try to understan them. you can find various ways that can be used to solve a task. from time to time i also take a look at my file list, open codes that i have done, but do not remember clearly. but the most important thing is practice. this does mean writing long or complex codes, but just solve something, try to improve it, also in terms of efficiency and memory consumption.
26th May 2022, 5:05 PM
Lothar
Lothar - avatar
+ 1
Lothar Thank you Lothar. I realize now that my regex code bit doesn't work for the other cases, either. I thought it did but the whole code worked because of the other predicate and conditional despite the regex. I have a question if you have time: I'd used isdigit once and forgot about it. I know that we should use cheat sheets, help, dir etc but how do I stop myself from forgetting that such functions even exist? When you know it does, you look up these sources but what do you do when you cannot remember even that? (considering other libraries) Does it come with practice, too? Are there methods you have found useful that newbies should know?
26th May 2022, 3:23 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Lothar Thank you so much for your detailed, helpful response to my extra question. I'd better organize my files so as to be able to do all this while I can, when they haven't piled up yet.
26th May 2022, 5:37 PM
Korkunç el Gato
Korkunç el Gato - avatar