Results 1 to 4 of 4

Thread: [RESOLVED] Parsing String to match my Pattern

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] Parsing String to match my Pattern

    I am developing a client/server application.
    How can I parse a string (message from client) to match with my required string pattern.
    Let say my pattern required is:

    RESULT Acct. Number Year Part
    Sample: RESULT 1063-12 2012 1st

    I need to check incoming message if it is in correct format. if not correct my server will reply a correction message.
    Server must accept correct format of message only. Coz strings from message is use to query my database for reply of result.

    I have no initial code for now here. Im just wondering an idea now how to do it in a code.

    I'm thinking if it is best to use regex split or match.

    Anyone have sample of working code using regex?
    Im also googling it now.

    Thanks.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Parsing String to match my Pattern

    try this:

    Code:
    Dim Sample As String = "RESULT 1063-12 2012 1st"
    'RESULT is immutable
    'acct number must be 4 digits followed by a hyphen followed by 2 digits
    'year must be  4 digits
    'last part can range from 1st to 9999th
    Dim rx As New Regex("^RESULT\s\d{4}\-\d{2}\s\d{4}\s\d{1,4}(st|nd|rd|th){1}$")
    MsgBox(rx.IsMatch(Sample))
    Last edited by .paul.; Aug 1st, 2012 at 02:42 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Parsing String to match my Pattern

    Thanks .paul.

    It works. I did some changes here is my modified code to work with me
    Code:
    Private Function isMatch(ByVal code As String) As Boolean
            Dim rx As New Regex("^GRADE\s\d{5}\s\d{4}\-\d{2}\s\d{1,4}(st|nd|rd|th){1}$")
            If rx.IsMatch(code) Then
                isMatch = True
            Else
                isMatch = False
            End If
    End Function
    But I'm thinking if I want to match with many pattern by using Select Case?
    Last edited by dr_aybyd; Aug 13th, 2012 at 09:55 PM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Parsing String to match my Pattern

    try this:

    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim code As String = "something"
    7.         Dim rxs() As Regex = {New Regex("pattern1"), New Regex("pattern2"), New Regex("pattern3")}
    8.         Select Case True
    9.             Case rxs(0).IsMatch(code)
    10.  
    11.             Case rxs(1).IsMatch(code)
    12.  
    13.             Case rxs(2).IsMatch(code)
    14.  
    15.         End Select
    16.     End Sub
    17.  
    18. End Class

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