views:

658

answers:

4

Hi,

i need a Regular Expression to convert a a string to a link.i wrote something but it doesnt work in asp.net.i couldnt solve and i am new in Regular Expression.This function converts (bkz: string) to (bkz: show.aspx?td=string)

Dim pattern As String = "<bkz[a-z0-9$-$&-&.-.ö-öı-ış-şç-çğ-ğü-ü\s]+)>"
Dim regex As New Regex(pattern, RegexOptions.IgnoreCase)
str = regex.Replace(str, "<a href=""show.aspx?td=$1""><font color=""#CC0000"">$1</font></a>")
A: 

Your regexp is in trouble because of a ')' without '('

Would:

&lt;bkz:\s+((?:.(?!&gt;))+?.)&gt;

work better ?

The first group would capture what you are after.

VonC
A: 

Thanks Vonc,Now it doesnt raise error but also When i assign str to a Label.Text,i cant see the link too.Forexample after i bind str to my label,it should be viewed in view-source ;

<span id="Label1">(bkz: <a href="http://www.mysite.com?t=here"&gt;here&lt;/a&gt;)&lt;/span&gt;

But now,it is in viewsource source;

<span id="Label1">(bkz: here)</span>
I am not sure what is still the problem here... Are you saying the result of regex.replace(str,...) is incorrect ?
VonC
i think,regex.replace doesnt replace.Because after i assign Label1.Text=str,i dont see the link.(i do it in CodeBehind).
I didnt see there was a ':' after bkz. Could you try my revised regex ? (the one with "bkz:")
VonC
Thanks VonC,After i tried it,it worked.Regards
+3  A: 

Generic remarks on your code: beside the lack of opening parentheses, you do redundant things: $-$ isn't incorrect but can be simplified into $ only. Same for accented chars.
Everybody will tell you that font tag is deprecated even in plain HTML: favor span with style attribute.

And from your question and the example in the reply, I think the expression could be something like:

\(bkz: ([a-z0-9$&.öışçğü\s]+)\)

the replace string would look like:

(bkz: <a href=""show.aspx?td=$1""><span style=""color: #C00"">$1</span></a>)

BUT the first $1 must be actually URL encoded.

PhiLho
By the way Philho, Ur solution works by deleting (bkz:).we see just string as Link between (bkz: and ).it is not problem for now but i wonder what we should do if we wanted to see (bkz:) too?
@Compiler: Regarding your pattern: I see that you are trying to match (turkish?) text. You should probably use \w in place of your manually selected characters: [\w\s$.]
Tomalak
A: 

Thanks VonC and Philho. Ur Solutions worked.

You are welcome. You should post your "answers" as comment to our answer though. See http://stackoverflow.com/questions/18557/how-does-stackoverflow-work-the-official-faq, especially http://stackoverflow.com/questions/132017/how-do-comments-work-in-stackoverflow
VonC