Results 1 to 11 of 11

Thread: [RESOLVED] Regular Expression help

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Resolved [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:
    Code:
    [PlayerID=12345]
    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

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    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:
    1. private void button1_Click(object sender, EventArgs e)
    2.         {
    3.             string s = "[PlayerID=12345]";
    4.             Regex patern = new Regex(@"\[(PlayerID=(\d+))\]");
    5.             MatchEvaluator matchEval = new MatchEvaluator(GetReplacement);
    6.  
    7.             s = patern.Replace(s,GetReplacement);
    8.  
    9.             MessageBox.Show(s);
    10.  
    11.         }
    12.  
    13.         private string GetReplacement(Match m)
    14.         {
    15.             return "<a href='somestaticwebaddress?" + m.Groups[1] + "'>" + m.Groups[2] + "</a>";
    16.         }

    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

  3. #3

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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

  4. #4
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    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

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Regular Expression help

    Quote Originally Posted by motil View Post
    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.

  6. #6
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: [RESOLVED] Regular Expression help

    Quote Originally Posted by motil View Post
    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 View Post
    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

  7. #7

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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

  8. #8
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    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

  9. #9

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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

  10. #10
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    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

  11. #11

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Regular Expression help

    Quote Originally Posted by plender View Post
    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
  •  



Click Here to Expand Forum to Full Width