, python, .
, , :
import re
strings = ["only screen and (min-width: 500px) and (max-width: 599px)", "only screen and (min-width: 500px)", "(max-width: 599px)"]
regex = re.compile(r'(min|max)-width:\s*(\d+)px(.*(?!\1)(max|min)-width:\s*(\d+)px)?')
for string in strings:
match = re.search(regex, string)
print
print string
if match:
term_1 = match.group(1)
value_1 = match.group(2)
term_2 = match.group(4)
value_2 = match.group(5)
print "Match!\n{} {}".format(term_1+"-width:", value_1)
if term_2:
print "{} {}".format(term_2+"-width:", value_2)
else:
print "Not a match"
, :
only screen and (min-width: 500px) and (max-width: 599px)
Match!
min-width: 500
max-width: 599
only screen and (min-width: 500px)
Match!
min-width: 500
(max-width: 599px)
Match!
max-width: 599
this. , lookahead ( lookahead) "match expr, ".
, lookahead , , , , , max min. , px .* . . , ?, , .