1

I have a string as such:

tempString = "65.00000000;ACCUMPOINTS;Double:0.0593000000;D"

And my output shld be "65.000000,0.0593000000" or at least give two separated values.

I am using regex to find the values in the string.

My code:

tempString = "65.00000000;ACCUMPOINTS;Double:0.0593000000;D"
temp = NumericOnly(tempString)

Public Function NumericOnly(s As String) As String
    Dim s2 As String
    Dim replace_hyphen As String
    replace_hyphen = " "
    Static re As VBScript_RegExp_55.RegExp
    If re Is Nothing Then Set re = New RegExp
    re.IgnoreCase = True
    re.Global = True
    re.Pattern = "[^\d+]" 'includes space, if you want to exclude space "[^0-9]"("-?\\d+");
    s2 = re.Replace(s, vbNullString)
    re.Pattern = "[^\d+]"
    NumericOnly = re.Replace(s2, replace_hyphen)
End Function

My output is like this:

"650000000000593000000"

How to go about doing this? Need some help.

1 Answer 1

2

Just did a minor change in your regex. Instead of just using [^\d+], now [^\d.:+] is being used to indicate that we would like one or more of digits, dots or colons. Then, colon is replaced with a comma to get the desired result.

Sub Test()
    Dim tempString As String
    tempString = "65.00000000;ACCUMPOINTS;Double:0.0593000000;D"
    temp = NumericOnly(tempString)
    MsgBox temp
End Sub

Public Function NumericOnly(s As String) As String
    Dim s2 As String
    Dim replace_hyphen As String
    replace_hyphen = " "
    Static re As VBScript_RegExp_55.RegExp
    If re Is Nothing Then Set re = New RegExp
    re.IgnoreCase = True
    re.Global = True

    re.Pattern = "[^\d.:+]"
    s2 = re.Replace(s, vbNullString)

    re.Pattern = "[^\d.:+]"

    NumericOnly = re.Replace(s2, replace_hyphen)
    NumericOnly = Replace(NumericOnly, ":", ",")
End Function
Sign up to request clarification or add additional context in comments.

5 Comments

jus a qn: when i tried "[\d+]", it was returning all the letters. Why is that so? i thought that \d looks for digits in a string right?
\d+ means one or more digits. So the RegEx stripped off everything except the numbers 0-9 and gave you 650000000000593000000. With \d.:+ we asked RegEx to keep 0-9, dot and colon but strip out everything else.
but u have entered "[^\d.:+]" not [\d.:+]. so where is the difference?
Correct usage is [^\d.:+]. I wrote \d.:+ in a hurry. [^\d.:+]s beauty lies in the ^ part of the RegEx. The caret symbol inverts the selection; meaning, it match all characters that are not a digit, dot or colon. That's how you can strip characters you don't want.
i thought it was re.Execute, didn't know it is re.Replace. Thanks a lot.

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.