# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"#{(\w+[.\w+]+)\}"
test_str = "The [#{product.name}](/card-details/card-name/#{details.slug.value}) which has an annual fee of #{details.annual_fee.value}, does double-duty as a rewards credit card and a balance transfer credit card. You’ll get a big sign-up bonus: Discover will automatically double cash back for new card holders in your first year. Plus, when you opt in to a bonus rewards program, this card earns 5% cash back on rotating bonus categories like gas and restaurants. Be aware that these 5% rewards are capped at $1,500 spent per quarter. On all other purchases, you’ll get 1% cash back. There is #{details.pros_bullets.value.1} and it comes with a generous 0% APR offer: #{details.apr_intro_message_list.value.0}. The card charges a 3% balance transfer fee, and Discover isn’t widely accepted abroad. But it could be a great choice if you’re trying to earn big rewards on new purchases while paying off an old debt."
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html