|
-
Apr 14th, 2010, 01:06 PM
#1
[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.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
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 
-
Apr 14th, 2010, 02:09 PM
#3
Re: Regular Expression help
Thanks a lot!
i really need to improve my regx skills..any good sources / books ?
anyway thanks again!
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Apr 14th, 2010, 02:10 PM
#4
Lively Member
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
I wrote a book  Visual Studio 2008 Programming
Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn
-
Apr 14th, 2010, 02:14 PM
#5
Re: Regular Expression help
 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.
-
Apr 14th, 2010, 02:22 PM
#6
Re: [RESOLVED] Regular Expression help
 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).
 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 
Yeah almost the same Regular expression.
Last edited by stlaural; Apr 14th, 2010 at 02:37 PM.
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 
-
Apr 14th, 2010, 02:23 PM
#7
Re: [RESOLVED] Regular Expression help
plender's example's failed but it just need a bit of tweaking
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Apr 14th, 2010, 02:27 PM
#8
Lively Member
Re: [RESOLVED] Regular Expression help
Bugger - the closing right angle bracket?
I wrote a book  Visual Studio 2008 Programming
Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn
-
Apr 14th, 2010, 02:31 PM
#9
Re: [RESOLVED] Regular Expression help
yea i think so, but as i said only some tweaking needed
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Apr 14th, 2010, 02:33 PM
#10
Lively Member
Re: [RESOLVED] Regular Expression help
What's a little tweaking between friends
I wrote a book  Visual Studio 2008 Programming
Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn
-
Apr 14th, 2010, 02:37 PM
#11
Re: [RESOLVED] Regular Expression help
 Originally Posted by plender
What's a little tweaking between friends 
heh
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
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
|