aspnetguy Posted February 16, 2007 Share Posted February 16, 2007 I have a string that could be like the following <script> function myFunction() { //some js code }</script><div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div> Note: this is a string variable. I need to write a regular expression to replace all { & } with <%= & %> but it can't touch anything between <script></script>Any help would be appreciated. Link to comment Share on other sites More sharing options...
aalbetski Posted February 16, 2007 Share Posted February 16, 2007 Try this, it could probably still be refined, but it seems to work based on your example given. using System;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { string sText = @"<script>function myFunction(){ //some js code } </script> <div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div>"; Regex reTextToChange = new Regex(@"</script>(?<text>.*$)"); Regex reTextNotToChange = new Regex(@"^.*?</script>"); string sTextNotToChange = reTextNotToChange.Match(sText).ToString(); string sTextToChange = reTextToChange.Match(sText).Groups["text"].ToString(); sTextToChange = sTextToChange.Replace(@"{", @"<%=").Replace(@"}", @"%>"); sText = sTextNotToChange + sTextToChange; } }} this result: <script>function myFunction(){ //some js code } </script> <div><%=sText1%><br><br><%=sText2%><br><br><%=PhotoGallery%></div> Link to comment Share on other sites More sharing options...
aspnetguy Posted February 16, 2007 Author Share Posted February 16, 2007 thank you that helpos however I should have given a better example. It could be like this <div>{sImage1}</div><script> function myFunction() { //some js code }</script><div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div> or even have multiple <script></script> mixed throughout the string.thank you though Link to comment Share on other sites More sharing options...
aalbetski Posted February 16, 2007 Share Posted February 16, 2007 A little more robust now. Should meet anything you throw at it. Of course, I only tested it on your example. using System;using System.Collections.Generic;using System.Text;using System.Text.RegularExpressions;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { string sText = @"<div>{sImage1}</div><script> function myFunction() { //some js code }</script><div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div>"; Regex reMatchPattern = new Regex(@"<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)</\1>"); StringBuilder sb = new StringBuilder(); MatchCollection matches = reMatchPattern.Matches(sText); foreach (Match rMatch in matches) { if (rMatch.ToString().StartsWith("<script>",true,null)) { sb.Append(rMatch.ToString()); } else { sb.Append(rMatch.ToString().Replace(@"{", @"<%=").Replace(@"}", @"%>")); } } sText = sb.ToString(); } }} <div><%=sImage1%></div><script> function myFunction() { //some js code }</script><div><%=sText1%><br><br><%=sText2%><br><br><%=PhotoGallery%></div> Link to comment Share on other sites More sharing options...
aspnetguy Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks for the help but I figured out a way without Regex. <%@ Page Language="C#" ValidateRequest="false"%><%@ Import Namespace="System.Text.RegularExpressions" %><%@ Import Namespace="System.Text" %><script runat="server"> private void Page_Load(object sender, EventArgs e) { txtInput.TextMode = TextBoxMode.MultiLine; txtOutput.TextMode = TextBoxMode.MultiLine; } private void btnClick(object sender, EventArgs e) { string input = txtInput.Text; input = input.Replace("<script","~<script").Replace("</" + "script>","</" + "script>~"); string[] parts = input.Split('~'); StringBuilder sb = new StringBuilder(); foreach(string part in parts) { if(part.IndexOf("<script") == -1) sb.Append(part.Replace("{","<%=").Replace("}","%>")); else sb.Append(part); } txtOutput.Text = sb.ToString(); }</script><html><head> <title></title></head><body><form runat="server" method="post"> <div>Input:</div> <asp:textbox id="txtInput" runat="server" style="width:400px;height:150px"/> <br><br> <div>Output:</div> <asp:textbox id="txtOutput" runat="server" style="width:400px;height:150px"/> <br><br> <asp:button id="btnFilter" text="Filter" runat="server" OnClick="btnClick"/></form><!----------------------------------SAMPLE INPUT-----------------------------------------{sText}<br><script src="somewhere/file.js"></script><script>function name(){ //js code}</script>{sText2}<br><div>{sImage1}</div><script> function name2() { //more code }</script>{sText3}--------------------------------------END SAMPLE INPUT------------------------------------></body></html> Link to comment Share on other sites More sharing options...
Yahweh Posted February 28, 2007 Share Posted February 28, 2007 I have a string that could be like the following<script> function myFunction() { //some js code }</script><div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div> Note: this is a string variable. I need to write a regular expression to replace all { & } with <%= & %> but it can't touch anything between <script></script>Any help would be appreciated. .Net has a fantastic regex construct called "balanced expressions" that are very useful for solving this problem. You only need a single regex.replace command to replace the text properly.Here is the expression I use:(?> <script> (?<LEVEL>) # On opening paren push level | </script> (?<-LEVEL>) # On closing paren pop level | \{(.*?)\} # Match any char except <script> or </script>)(?(LEVEL)(?!)) # If level exists then fail Feel free to test this expression at Regex Tester.The VB.Net code for it looks like this: Private Function myFunction(ByVal strInput As String) As String Dim strExpression As String = _ "(?>" & vbNewLine & _ " <script> (?<LEVEL>) # On opening paren push level" & vbNewLine & _ " |" & vbNewLine & _ " </script> (?<-LEVEL>) # On closing paren pop level" & vbNewLine & _ " |" & vbNewLine & _ " \{(.*?)\} # Match any char except <script> or </script>" & vbNewLine & _ ")" & vbNewLine & _ "(?(LEVEL)(?!)) # If level exists then fail" & vbNewLine Return System.Text.RegularExpressions.Regex.Replace(strInput, strExpression, "<%=$1%>", System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace).ToString() End Function Input: {something} here<script> function myFunction() { //some js code }</script><div>{sText1}<br><br>{sText2}<br><br>{PhotoGallery}</div><script> function anotherFunction() { //some js code }</script>{rock} Output: <%=something%> here<script> function myFunction() { //some js code }</script><div><%=sText1%><br><br><%=sText2%><br><br><%=PhotoGallery%></div><script> function anotherFunction() { //some js code }</script><%=rock%> Link to comment Share on other sites More sharing options...
aspnetguy Posted February 28, 2007 Author Share Posted February 28, 2007 thanks, where were 12 days ago j/k thanks very much Link to comment Share on other sites More sharing options...
jesh Posted February 28, 2007 Share Posted February 28, 2007 Brilliant. Thanks for posting the link to that article! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now