# 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 = (" @foreach($fsn_uploaded_data as $client_no => $client)\n"
" <div class=\"client\" style=\"border: 1px solid #888; margin-bottom: 10px;\">\n"
" <h2>{{ $client['client_name'] or '-' }}</h2>\n"
" <p>{{ $client->postal_address or '-' }}, {{ $client->zip_code_postcode or '-' }}, {{ $client->city or '-' }}, {{ $client->countr_code or '-' }}</p>\n"
" }\n\n"
"{{-- @foreach($client->delivery_addresses as $delivery_no => $address)\n"
" <div class=\"delivery_addresses\" style=\"border: 1px solid #ddd; margin: 5px;\">\n"
" <h3>{{ $address->client_delivery_name or '-' }}</h3>\n"
" <p>{{ $address->delivery_address or '-' }}, {{ $address->zip_code or '-' }}</p>\n"
" </div>\n"
" @endforeach --}}\n"
" </div>\n"
" @endforeach")
subst = "$\\1['\\2']"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
# 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