Skip to content Skip to sidebar Skip to footer

Why Is The Allowregexes Keyword In Pyinputplus Allowing Abc In Pyip.inputnum(allowregexes=[r'(c)+'])?

Working through Automate the Boring Stuff, and can't see why allowRegexes keyword in pyinputplus doesn't seem to be working for me. for example >>> import pyinputplus as p

Solution 1:

The issue is either within your assumption of how the parameter allowRegexes works, or in your regex. Though I guess it's probably the former.

The behavior is this: If the regex set in allowRegexes matches any part of the input, the whole string will be valid (respectively invalid, if used in blockRegexes). The regex does not need to match the entire input. Granted, that may not be completely clear from the wording in the documentation, but the examples listed in the description of pyinputplus.inputInt() illustrate this behavior.

Since (C)+ will match any C in a string, any sequence that contains a C anywhere will pass validation, as you observed. If you wanted only sequences of C without spaces, like C, CC, CCC, etc., to be added as valid input, your regex could e.g. look like this: ^C+$. See https://regex101.com/r/x03Fes/1

>>> import pyinputplus as pyip
>>> pyip.inputNum('input: ', allowRegexes=[r'^C+$'])
input: ABC
'ABC'isnot a number.
input: ACB
'ACB'isnot a number.
input: CAB
'CAB'isnot a number.
input: CAC
'CAC'isnot a number.
input: CCC CCC
'CCC CCC'isnot a number.
input: CCC
'CCC'

Post a Comment for "Why Is The Allowregexes Keyword In Pyinputplus Allowing Abc In Pyip.inputnum(allowregexes=[r'(c)+'])?"