Solved

How to validate a comma delimited string with Regex

Posted on 2006-06-26
3
485 Views
Last Modified: 2008-03-06
How to use RegEx to validate a string in the format of

12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C

The valid format would be any number of numerical value + "C;" as the delimiter except the last element ends with "C"

Thanks.
0
Comment
Question by:cyn_jiang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16988412
1) That looks like semi-colon delimited text, and not comma-delimited

2) Try this:

Imports System.Text.RegularExpressions

    If ValidateInput("12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C") Then
      MessageBox.Show("Valid")
    End If

  End Sub

  Private Function ValidateInput(ByVal input As String) As Boolean
    Dim pattern As String = "^\d*C$"

    Dim elements As String() = input.Split(";")

    For Each element As String In elements

      If Not Regex.IsMatch(element, pattern) Then
        MessageBox.Show("Element is invalid:  " & element)
        Return False
      End If

    Next element

    Return True

  End Function

Bob
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 total points
ID: 16988812
Hi cyn_jiang;

This sample code will do what you want.

Imports System.Text.RegularExpressions

        ' Sample Data
        Dim data As String = "12C;325252C;99999C;378C;28C;3535345342C;1C;56896745C"
        ' Regex pattern to validate data
        Dim pattern As String = "^(\d+C;|\d+C)+$"

        ' Check to see if data is valid
        If Regex.IsMatch(data, pattern) Then
            MessageBox.Show("The data is vaild")
        End If


Fernando
0
 

Author Comment

by:cyn_jiang
ID: 16991933
Sorry, it is semi-colon delimited text, I would allow them to input space in between the elements.

So I modified Fernando's code a little bit.

Imports System.Text.RegularExpressions
 
Dim pattern as String = "^(\d+(C; *)|\d+C)+$"

      If Regex.IsMatch(data, pattern) Then
            MessageBox.Show("The data is vaild")
        End If

Thanks for your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question