I need to validate the request Accept header using regex (python). The regex must match application/json or application/jose+jwe and additional parameters (charset=utf8 and q value).
Originally the requirements to only accept application/json and q value and no other mimi types. I had the following regex that worked.
I know need to be able to include the charset paremeter to
charset=utf8 and be able to match anywhere in the line.
I am new to regex and created the following regex but it does not match all the requirements (https://regex101.com/r/vFMCcI/11) -
(application/json; q=(0|1|(0\.[1-9])))$|(application\/json; charset=utf8)|(application\/json; charset=utf8 q=(0|1|(0\.[1-9])))|(application/json)
The test strings are
application/json, application/json; q=0.2 application/json; charset=utf8 application/json; q=0.2 charset=utf8 application/json; charset=utf8 q=0.2 text/html, application/json; q=0.2, application/pdf application/jose+jwe application/jose+jwe; q=0.2 application/jose+jwe; charset=utf8 application/jose+jwe; q=0.2 charset=utf8 application/jose+jwe; charset=utf8 q=0.2 text/html, application/jose+jwe; q=0.2, application/pdf
- Why am I only getting partial matches for
application/json; charset=utf8 q=0.2?
- The regex is getting too long when application/jose+jwe is not even included. It is adding on ms to the requests. Any pointers on this can be better optimised?
Q value must be 0-1 and only to 1 decimal place
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
The only acceptable charset value is