import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\$([\\w]+)->([^\\W]+)";
final String string = " @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";
final String subst = "$\\1['\\2']";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + 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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html