I’m guessing the use case would be a situation where you’re already sure that the input matches the pattern, and you just want to extract one or more substrings decided by the pattern; or where if the input doesn’t match you want to reject the input (like the OP suggested).
What makes the API design a bit problematic is that we’d have to fix which exception to raise (ValueError
? Something specific to the re
module?) or we’d have to specify the exception explicitly in the call, which would make it pretty cumbersome.
If you feel this need regularly you should probably just break down and put a little helper in your personal toolbox.