tags:

views:

152

answers:

3

Hi,

I want to convert a link to a definite text by using regular expression.While binding datagrid,i have a function which convert (look: the text) to link.My Function is here.

Private Function Convertlook(ByVal str As String) As String

        Dim look As String
        Dim pattern As String = "\(look: ([a-z0-9$&.öışçğü\s]+)\)"
        Dim regex As New Regex(pattern, RegexOptions.IgnoreCase)
        Dim htmlanc As New System.Web.UI.HtmlControls.HtmlAnchor()
        Dim postbackRef As String = Page.GetPostBackEventReference(htmlanc, "$1")
        htmlanc.HRef = postbackRef
       str = regex.Replace(str, "(look: <a href=""javascript:" & htmlanc.HRef & """><font color=""#CC0000"">$1</font></a> )")      
        look = str
        Return look 

end function

The problem is that i want to edit the text,how can i reverse it to (look: the text)?Should i use again regular expression and what can be right regular expression of it?

+1  A: 

Looks like the regex can be reduced to 'anything between the angle brackets'

Dim regex As New Regex(".*>(.*)</font.*", RegexOptions.IgnoreCase)
str = regex.Replace(str, "(look: $1)")
soulmerge
Thanks to everybody.Soulmerge's solution worked.
A: 

Wouldn't it be easier to keep both the converted and the unconverted version of the text (ie. in the ViewState or ControlState)? That would save you a lot of trouble. What would happen if your original text contains a string like '<font'?

I suggest: don't go there, not worth the effort. Keep track of the source.

Teun D
believe me,it is better to use regular expression in this scenerio :)
A: 

I changed your code in C#, here is what you want :

string str = "(look: trialText)";
string look = string.Empty;
string pattern = @"\(look: ([a-z0-9$&.öışçğü\s]+)\)";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
System.Web.UI.HtmlControls.HtmlAnchor htmlanc = new System.Web.UI.HtmlControls.HtmlAnchor();
string postbackRef = Page.GetPostBackEventReference(htmlanc, "$1");
htmlanc.HRef = postbackRef;

// Here I capture the text inside the anchor :
Match matchedText = regex.Match(str);
string textInsideLink = regex.Replace(matchedText.Value, "$1"); // textInsideLink = "trialText"

str = regex.Replace(str, "(look: <a href=\"javascript:" + htmlanc.HRef + "\"><font color=\"#CC0000\">$1</font></a> )");

// I replace captured text with another text :
str = Regex.Replace(str, "(" + textInsideLink + ")", "anotherTextInsideLink"); 
// str = "(look: <a href=\"javascript:__doPostBack('','anotherTextInsideLink')\"><font color=\"#CC0000\">anotherTextInsideLink</font></a> )"
Canavar