[RESOLVED] Regular Expression help
Hi
well i have kinda complicated Regular Expression i need to do if some one can help me with this it'll be great..
basically this is what I need to do:
i have this string:
i need to find any number of this string and replace them to hyperlink like so:
Code:
<a href='somestaticwebaddress?PlayerID=12345'>12345</a>
the address querystring parameter and the hyperlink title should be the "12345" in the found string
this is what i have so far:
Code:
Regex reg = new Regex(@"^\[PlayerID=");
ltl.Text = (reg.Replace("[PlayerID=123]","<a href=''>")).ToString();
this is the first i'm trying Regex ...
any ideas ?
Thanks.
Re: Regular Expression help
Hey motil, here's a good way to do it, I did it in C# as your code sample seemed to be in C#:
csharp Code:
private void button1_Click(object sender, EventArgs e)
{
string s = "[PlayerID=12345]";
Regex patern = new Regex(@"\[(PlayerID=(\d+))\]");
MatchEvaluator matchEval = new MatchEvaluator(GetReplacement);
s = patern.Replace(s,GetReplacement);
MessageBox.Show(s);
}
private string GetReplacement(Match m)
{
return "<a href='somestaticwebaddress?" + m.Groups[1] + "'>" + m.Groups[2] + "</a>";
}
Basically in m.Groups[0] you get the complete match, in Groups[1] and Groups[2] you get the part of the match that are placed in parenthesis in the declared patern. Have a look at the MSDN page on MatchEvaluator, its very useful when it comes to using RegEx to replace values.
EDIT : I should've used StringBuilder in GetReplacement instead of concatenation operators.
Have fun with the RegEx !
Re: Regular Expression help
Thanks a lot!
i really need to improve my regx skills..any good sources / books ?
anyway thanks again!
Re: [RESOLVED] Regular Expression help
And my stab at it :)
Code:
Private r As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("\d+", System.Text.RegularExpressions.RegexOptions.Compiled)
Sub Main()
Dim x = "[PlayerID=12345]"
Dim n = Val2(x)
Dim out = String.Format("<a href=""something.aspx?PlayerID={0}""n</a>", n)
Stop
End Sub
Function Val2(ByVal Input As String) As Integer?
If r.IsMatch(Input) Then
Return r.Match(Input).ToString
End If
End Function
Though by the looks of it it's the same regular expression :D
Re: Regular Expression help
Quote:
Originally Posted by
motil
i really need to improve my regx skills..any good sources / books ? !
I've always found http://www.regular-expressions.info/ to be an excellent source.
Re: [RESOLVED] Regular Expression help
Quote:
Originally Posted by
motil
Thanks a lot!
i really need to improve my regx skills..any good sources / books ?
anyway thanks again!
You're welcome.
sorry I mostly used MSDN documentation (Links in my signature).
Quote:
Originally Posted by
plender
And my stab at it :)
Code:
Private r As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("\d+", System.Text.RegularExpressions.RegexOptions.Compiled)
Sub Main()
Dim x = "[PlayerID=12345]"
Dim n = Val2(x)
Dim out = String.Format("<a href=""something.aspx?PlayerID={0}""n</a>", n)
Stop
End Sub
Function Val2(ByVal Input As String) As Integer?
If r.IsMatch(Input) Then
Return r.Match(Input).ToString
End If
End Function
Though by the looks of it it's the same regular expression :D
Yeah almost the same Regular expression.
Re: [RESOLVED] Regular Expression help
plender's example's failed but it just need a bit of tweaking :)
Re: [RESOLVED] Regular Expression help
Bugger - the closing right angle bracket?
Re: [RESOLVED] Regular Expression help
yea i think so, but as i said only some tweaking needed :)
Re: [RESOLVED] Regular Expression help
What's a little tweaking between friends :D
Re: [RESOLVED] Regular Expression help
Quote:
Originally Posted by
plender
What's a little tweaking between friends :D
heh :lol: