r/PHPhelp • u/lindymad • Apr 07 '25
Checking if a user-supplied regular expression will only match a number
My situation is as follows:
A user can enter a custom regular expression that validates a field in a form they have created in our system.
I need to know whether that regular expression means that the field validation optionally requires an integer or a decimal. By "optionally" here I mean if the regex accepts blank or an integer or decimal, that would count for my purposes.
The reason is that eventually a temporary database table is constructed and if I know that the only valid values will be integers, I want to make the database field type an INT. If I know that the only valid values will be decimals (or integers), I want to make the database field type a FLOAT. In all other circumstances, the database field type will be TEXT. If the validation allows no value to be entered, it will be a NULL field, if not it will not allow NULL. I know how to check for this already (that's easy - if (preg_match('/'.$sanitizedUserEnteredRegex.'/', '')) // make it a NULL field)
I have no control over what regular expression is entered by a user, so examples of regular expressions that only match an integer could be as simple as /^\d*$/, or as crazy as e.g. /^-?([1-4]+)5\d{1,3}$/. That means I can't just check if a random number happens to match or a random string happens not to match, in the same way I can check for if no value is allowed.
The two things I need help with are:
- How can I determine whether a regular expression will only match an integer.
- How can I determine whether a regular expression will only match an integer or a decimal.
I am aware of the various sanitation requirements of using a user supplied regular expression and it's eventual translation into a database table, I'm not looking for help or advice on that side of things.
Thanks
View all comments
Show parent comment
1
u/lindymad Apr 07 '25 edited Apr 07 '25
I agree, but research needs to be done to determine whether or not what was asked is not a good solution
I disagree. If it can be done reliably this way, it would be the best solution. Whether it can be done reliably this way is why I came here, to seek help in finding that out.
If there is a way to determine whether a regex will only match a number, that would be far, far easier than changing the whole way the system works. It would be a simple code change from one team (the one I'm part of) in the
is_validation_numeric($validationRegex)function, instead of a bunch of changes to the front end, api, backend processes, and end user documentation, which would mean changes from three different teams (api and backend processes are handled by the same team).