0

I have the below code that keeps the numbers in a string. It keeps the points regardless, but I want to keep the points IF THEY ARE FOLLOWED BY A DIGIT. So the function should give me the following results. What should I change in the regular expression pattern to get such results?

Input | Output

. . 0.236 | 0.236

bfbv0.011 | 0.011

. ..11 | 0.11

. rty12.45dt | 12.45

qw-23.25 | -23.25

was 12.52. | 12.52

will be +336 | 336

Code:

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 = "[^0-9.-]+"
    s2 = re.Replace(s, vbNullString)

    NumericOnly = re.Replace(s2, replace_hyphen)
End Function
Community
  • 1
  • 1
Ibo
  • 4,081
  • 6
  • 45
  • 65

1 Answers1

5

-?\d*\.?\d+ achieves all your needs, but some later processing is needed to insert the leading zero when necessary. The pattern includes an optional leading minus sign and an optional dot but if there's a dot it must be followed by some digits or it will be ignored.

  Public Function NumericOnly(s As String) As String
      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*\.?\d+"
       End If

      If re.Test(s) Then
          NumericOnly = re.Execute(s)(0)
          If Left(NumericOnly, 1) = "." Then NumericOnly = "0" & NumericOnly ' <-- to add leading 0 if necessary
          If Left(NumericOnly, 2) = "-." Then NumericOnly = "-0." & Mid(NumericOnly, 3) ' <-- to add leading 0 if necessary
      End If
  End Function

  Sub Test()
    Dim s
    For Each s In Split("abc, 123, 123.0, 0.00, -.01, bfbv0.011,. ..11,. rty12.45dt,qw-23.25,was 12.52. ,will be +336", ",")
      Debug.Print s, vbTab, NumericOnly(CStr(s))
    Next
  End Sub

Output

 abc            
 123                123
 123.0              123.0
 0.00               0.00
 -.01               -0.01
 bfbv0.011          0.011
. ..11              0.11
. rty12.45dt        12.45
qw-23.25            -23.25
was 12.52.           12.52
will be +336         336
A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • Usually, it's better to write a regex that parses a numeric value just like a language would. This `-?\d*\.?\d+` does not fill the bill though. In an effort to avoid matching the empty string, you've made the last `\d+` mandatory. Unfortunately, this will never match valid numbers that end in a decimal point like `27.` You can make it complete using a standard language numeric processing regex `(?:\d+(?:\.\d*)?|\.\d+)` –  Jun 03 '17 at 07:28
  • @sln, the answer's pattern parses `27.` into `27` (as stated it just ignores the dot in that case, which is correct). Not that it is unbeatable or standard (I'm almost sure there's something considered *"standard"* out there) but for the time being I would like to have a real counter-example. Besides, your suggested pattern does not include the leading `-` to extract from the sentence as required in the OP (probably easy to add?) – A.S.H Jun 03 '17 at 07:36
  • Is not including the leading negative sign take away the core regex I showed you ? Come on.. There is a big ass difference between `27` and `27.` in number land, one is a float, one is an integer. –  Jun 03 '17 at 07:44
  • @sln of course! For a programming language, even one that only has doubles as numbers (like Excel), the main issue is that `.` must be consumed within the token or else it would stay trailing and generates a syntax error. That's the big ass difference with the question's requirement, which is only to extract the number from the sentence ;) – A.S.H Jun 03 '17 at 07:48
  • Yeah, its moot. You don't know where a number is in a sentence. And, in fact, you can't tell the difference between a sentence period, either before or after some digits. You interpret the OP's question as giving meaning to a period before the number, but on the other hand, don't give a rat's ass about it being after some numbers... yeah, yeah, yeah .. right . –  Jun 03 '17 at 08:00