Regular expression in

ross13 used Ask the Experts™

I hope that someone is able to help. I am looking at writing a reg expression to try and find the first numeric value in a string (values from 0-9).

This string is going to be a postcode field so i need to find the first numeric value for the trim. This will either be a one or two value string field.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
May be OFF the mark, but you may be interested in a UK postcode validator:
#Region "UK_Post_Code_Validator"

    Function ValidateUKPostCode(ByVal PostCode As String) As Boolean

        Dim PostCodePattern = "(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})"
        'PostCodePattern = "/^(GIR 0AA|BFPO ([0-9]{1,4})|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})$/"
        Dim xMatch As New Regex(PostCodePattern, RegexOptions.IgnoreCase)
        If Not xMatch.Match(PostCode).Success Then
            Return False
            Return True
        End If

    End Function

#End Region

Open in new window

Don't really know what kind of regular expression you wish to build but you can get the position by something like this otherwise:

   Private Function CheckPosition(ByVal Mystring As String) As Short
        Dim z, y, x As Integer
        y = TextBox1.Text.Length
        For x = 0 To y - 1
            If IsNumeric(TextBox1.Text.Substring(x, 1)) Then
                y = x + 1
                Exit For
            End If
        Return y
    End Function
Top Expert 2011
Dim rx As Regex = new Regex("\d")
Dim start As Integer
Dim test As String = "Test 01234"

start = rx.Match(test).Index


' Output
' 01234

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial