0

I'm trying to make a vb function that takes as input a String and returns, if exist, the string made of numeric digits from the beginning until the first non numerical char, so:

123 -> 123
12f -> 12
12g34 -> 12
f12 -> ""
"" -> ""

I wrote a function that incrementally compares the result matching the regex, but it goes on even on non numeric characters...

This is the function:

Public Function ParseValoreVelocita(ByVal valoreRaw As String) As String

        Dim result As New StringBuilder
        Dim regexp As New Regex("^[0-9]+")
        Dim tmp As New StringBuilder
        Dim stringIndex As Integer = 0
        Dim out As Boolean = False

        While stringIndex < valoreRaw.Length AndAlso Not out
            tmp.Append(valoreRaw.ElementAt(stringIndex))
            If regexp.Match(tmp.ToString).Success Then
                result.Append(valoreRaw.ElementAt(stringIndex))
                stringIndex = stringIndex + 1
            Else
                out = True
            End If
        End While

        Return result.ToString

    End Function

The output always equals the input string, so there's something wrong and I can't get out of it...

3 Answers 3

3

Here's a LINQ solution that doesn't need regex and increases readability:

Dim startDigits = valoreRaw.TakeWhile(AddressOf Char.IsDigit)
Dim result As String = String.Concat(startDigits)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this instead. You need to use a capture group:

Public Function ParseValoreVelocita(ByVal valoreRaw As String) As String

    Dim result As New StringBuilder
    Dim regexp As New Regex("^([0-9]+)")
    Dim tmp As New StringBuilder
    Dim stringIndex As Integer = 0
    Dim out As Boolean = False

    While stringIndex < valoreRaw.Length AndAlso Not out
        tmp.Append(valoreRaw.ElementAt(stringIndex))
        If regexp.Match(tmp.ToString).Success Then
            result.Append(regexp.Match(tmp.ToString).Groups(1).Value)
            stringIndex = stringIndex + 1
        Else
            out = True
        End If
    End While

    Return result.ToString

End Function

The expression:

Dim regexp As New Regex("^([0-9]+)")

and the result appending lines have been updated:

result.Append(regexp.Match(tmp.ToString).Groups(1).Value)

1 Comment

Ok, this didn't work because it always appended the whole tmp String. Clearing the result before the append solved the issue, but I don't know if it's the most efficient way.
0

You have made your code very complex for a simple task.

Your loop keeps trying to build a longer string and it keeps checking if it is still working with digits, and if so keep appending results.

So and input string of "123x" would, if your code worked, produce a string of "112123" as output. In other words it matches the "1", then "12", then "123"and concatenates each before exiting after it finds the "x".

Here's what you should be doing:

Public Function ParseValoreVelocita(valoreRaw As String) As String
    Dim regexp As New Regex("^([0-9]+)")
    Dim match = regexp.Match(valoreRaw)
    If match.Success Then
        Return match.Groups(1).Captures(0).Value
    Else
        Return ""
    End If
End Function

No loop and you let the regex do the work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.