If I understand the question correctly, you want to check - given some regular expression r - if r might match anything which is not a number. In that sense, the empty regex $^
(matching nothing) would pass the test, since it matches nothing (and hence no non-number). 1[0-9][0-9a-z]
, however, matches 10s
, which is not a number, hence the test fails.
That is not possible with Python's regex facilities. You would need a regex language which supports intersection (&), complement (~), and test for non-emptiness (e.g., through generation of a matching word). Then, if r is your regex, you would need to check if
`r & ~(0|[1-9][0-9]*)`
is non-empty.
Intersection and complement are computationally expensive, but there are some regex libraries that support them. An example (Java) which I know of is the BRICS automaton/regex library.
This could be realized as follows (assuming you're adhering to the BRICS regex syntax):
// Checks if `re` might match a non-number, and returns an example; otherwise, null is returned
public String matchesNonNumber(String re) {
// construct regex like above
RegExp bricsRe = new RegExp("(" + re + ") & ~(0|[1-9][0-9]*)", RegExp.INTERSECTION | RegExp.COMPLEMENT);
Automaton a = bricsRe.toAutomaton();
return a.getShortestExample(true); // returns shortest accepted string, or null if no string is accepted
}
See the JavaDoc of the RegExp and Automaton classes. This is not an example matching the python tag of the question, but the problem you want to solve also is not inherently language-specific.