|
-
Apr 14th, 2010, 01:54 PM
#2
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 !
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|