-1

Wondering if this is a correct way to use Regex in Python. Basically I want to be able to set CORS headers with a regex pattern.

My URL string is https://([a-zA-Z0-9-]+[.])*some.subdomain.com

Will this parse correctly during run time and will it accommodate any letter plus a dash before "some"?

Will headers be correctly set? For example when a user gets to

https://e-g.some.subdomain.com

will CORS header that was set with the Regex pattern allow this URL to work?

Edit:

After some research, do I need to use the 'r'

SOME_URL = r'https://([a-zA-Z0-9-]+[.])*some.subdomain.com'

Henry Lee
  • 147
  • 1
  • 1
  • 8

1 Answers1

0

The head and tail are pretty much constant, so these can just be matched as simple strings.

import sys
import re

pattern = re.compile("https://[a-zA-Z0-9-]+\.some.subdomain.com")

del(sys.argv[0]) # remove the exename
for url in sys.argv:
    print("URL: [" + url + "] -> "+ str(pattern.match(url) != None))

Which Gives:

$ python py_regex.py https://blah.some.subdomain.com https://w#e.some.subdomain.com https://e-g.some.subdomain.com https://another-domain.com
URL: [https://blah.some.subdomain.com] -> True
URL: [https://w#e.some.subdomain.com] -> False
URL: [https://e-g.some.subdomain.com] -> True
URL: [https://another-domain.com] -> False
Kingsley
  • 14,398
  • 5
  • 31
  • 53