[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.
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))
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?
Re: Parsing String to match my Pattern
try this:
vb.net Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim code As String = "something"
Dim rxs() As Regex = {New Regex("pattern1"), New Regex("pattern2"), New Regex("pattern3")}
Select Case True
Case rxs(0).IsMatch(code)
Case rxs(1).IsMatch(code)
Case rxs(2).IsMatch(code)
End Select
End Sub
End Class