Created
June 10, 2014 11:47
-
-
Save dawngerpony/bc44f597efe48ba2ea9a to your computer and use it in GitHub Desktop.
Python regex checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# regex_checker.py | |
import re | |
expression = "setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '31.221.70.151', 10);" | |
# pattern = re.compile("^setCookie\('([^']+)', '([^'])', [0-9]+\);$") | |
pattern = re.compile("^setCookie\('([^']+)'.*$") | |
print pattern | |
matches = pattern.search(expression) | |
print "All: %s" % matches.group() | |
print "First match: %s" % matches.group(0) | |
print "Second match (name of cookie): %s" % matches.group(1) | |
cookieName = matches.group(1) | |
print "Cookie name: %s" % cookieName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have updated the script to return all three groups from the expression:
regex_checker.py
import re
expression = "setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '31.221.70.151', 10);"
pattern = re.compile("^setCookie('([^']+)', '([^']+)', ([0-9]+)).*$")
print pattern
matches = pattern.search(expression)
print "All: %s" % matches.group()
print "First match: %s" % matches.group(0)
print "Second match (name of cookie): %s" % matches.group(1)
print "Third match (value of cookie): %s" % matches.group(2)
print "Fourth match (No. of days when cookie expires): %s" % matches.group(3)
cookieName = matches.group(1)
cookieValue = matches.group(2)
cookieExpiryDate = matches.group(3)
print "Cookie name: %s" % cookieName
print "Cookie value: %s" % cookieValue
print "No. of days when cookie expires: %s" % cookieExpiryDate
which returns:
(.env)[vagrant@localhost vagrant]$ python bc44f597efe48ba2ea9a/regex_checker.py
<_sre.SRE_Pattern object at 0x172a710>
All: setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '31.221.70.151', 10);
First match: setCookie('YPF8827340282Jdskjhfiw_928937459182JAX666', '31.221.70.151', 10);
Second match (name of cookie): YPF8827340282Jdskjhfiw_928937459182JAX666
Third match (value of cookie): 31.221.70.151
Fourth match (No. of days when cookie expires): 10
Cookie name: YPF8827340282Jdskjhfiw_928937459182JAX666
Cookie value: 31.221.70.151
No. of days when cookie expires: 10