import Foundation
let pattern = #"\$([\w]+)->([^\W]+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = ##"""
@foreach($fsn_uploaded_data as $client_no => $client)
<div class="client" style="border: 1px solid #888; margin-bottom: 10px;">
<h2>{{ $client['client_name'] or '-' }}</h2>
<p>{{ $client->postal_address or '-' }}, {{ $client->zip_code_postcode or '-' }}, {{ $client->city or '-' }}, {{ $client->countr_code or '-' }}</p>
}
{{-- @foreach($client->delivery_addresses as $delivery_no => $address)
<div class="delivery_addresses" style="border: 1px solid #ddd; margin: 5px;">
<h3>{{ $address->client_delivery_name or '-' }}</h3>
<p>{{ $address->delivery_address or '-' }}, {{ $address->zip_code or '-' }}</p>
</div>
@endforeach --}}
</div>
@endforeach
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$\1['\2']"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression