1. Locate the content between <% $ foreach $ %> and <% $ endforeach $ %>.
(? <=\\%\$ \ S * foreach \ s * \$ \%\>) [. \ s] * (? ==<\%\$ \ S * endforeach \ s * \$ \%\>)
Example:
Input: <% $ foreach $ %> <asp: boundfield datafield = "<% $ columnname $ %>" headertext = "<% $ columndescription $ %>"/> <% $ endforeach $ %> aaa
Output: <asp: boundfield datafield = "<% $ columnname $ %>" headertext = "<% $ columndescription $ %>"/>
2. Locate the content between <% $ foreach $ %> and <% $ endforeach $ %>, and contain the matching strings
(? = \ <\ % \ $ \ S * foreach \ s * \ $ \ % \>) [. \ s] * (? <=\<\%\$ \ S * endforeach \ s * \$ \% \>)
Example:
Input: <% $ foreach $ %> <asp: boundfield datafield = "<% $ columnname $ %>" headertext = "<% $ columndescription $ %>"/> <% $ endforeach $ %> aaa
Output: <% $ foreach $ %> <asp: boundfield datafield = "<% $ columnname $ %>" headertext = "<% $ columndescription $ %>"/> <% $ endforeach $ %>