Extract Number From String

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Extract Number From String

Post by collectordave »

Just a quick question in case anyone has done it allready.

I recieve several strings in this format

"abc123ab"
"123bs"

etc

The letters either end can be any letter up to a maximum of three letters per string each end but could be no letters and only the number is significant as it is used to sort a whole structured array.

Is there an easy way to extract just the number into an integer variable?

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Extract Number From String

Post by RSBasic »

Code: Select all

EnableExplicit

Procedure.s StringToNumberString(String$)
  Protected a
  Protected CurrentCharacter$
  Protected NumberString$
  
  For a=1 To Len(String$)
    CurrentCharacter$ = Mid(String$, a, 1)
    If Asc(CurrentCharacter$) > 47 And Asc(CurrentCharacter$) < 58
      NumberString$ + CurrentCharacter$
    EndIf
  Next
  
  ProcedureReturn NumberString$
EndProcedure

Debug StringToNumberString("abc0123ab")
Debug StringToNumberString("0123bs")
Or:

Code: Select all

EnableExplicit

Procedure StringToNumber(String$)
  Protected a
  Protected CurrentCharacter$
  Protected NumberString$
  
  For a=1 To Len(String$)
    CurrentCharacter$ = Mid(String$, a, 1)
    If Asc(CurrentCharacter$) > 47 And Asc(CurrentCharacter$) < 58
      NumberString$ + CurrentCharacter$
    EndIf
  Next
  
  ProcedureReturn Val(NumberString$)
EndProcedure

Debug StringToNumber("abc123ab")
Debug StringToNumber("123bs")
\\Edit:
Or you use this RegEx code: (\d+)

Code: Select all


If CreateRegularExpression(0, "(\d+)")
  Dim Result$(0)
  NbFound = ExtractRegularExpression(0, "abc123ab", Result$())
  For k = 0 To NbFound-1
    Debug Result$(k)
  Next
Else
  Debug RegularExpressionError()
EndIf
Image
Image
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Extract Number From String

Post by collectordave »

Excellent All good

Thanks

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Extract Number From String

Post by Marc56us »

Other regex

Code: Select all

EnableExplicit

Enumeration 
     #Regex
EndEnumeration

Procedure.i Extract_Number(Str_Input.s)
     CreateRegularExpression(#Regex, "\d+")
     If ExamineRegularExpression(#Regex, Str_Input)
          While NextRegularExpressionMatch(#Regex)
               ProcedureReturn Val(RegularExpressionMatchString(#Regex))
          Wend          
     EndIf
     ; FreeRegularExpression(#Regex) ; Not needed here because working in Procedure will auto free ressources when leave
EndProcedure

Debug Extract_Number("abc123ab")
Debug Extract_Number("123bs")
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: Extract Number From String

Post by nco2k »

@RSBasic
never use a function as the -To- expression. it will be executed every single time:

Code: Select all

Procedure Len2(String$)
  Debug String$
  ProcedureReturn Len(String$)
EndProcedure

For i = 1 To Len2("Test") : Next

Debug "---"

Len2 = Len2("Test")
For i = 1 To Len2 : Next
also iterating through strings like that, is very slow and inefficient. pointers should be a LOT faster:

Code: Select all

Procedure.q StringToNumber(*String.Character)
  Protected Result.q, *Number, Length
  While *String\c
    If *String\c >= '0' And *String\c <= '9'
      If *Number = #False
        *Number = *String
      EndIf
      Length + 1
    ElseIf *Number
      Break
    EndIf
    *String + SizeOf(Character)
  Wend
  If *Number
    Result = Val(PeekS(*Number, Length))
  EndIf
  ProcedureReturn Result
EndProcedure

Debug StringToNumber(@"abc123ab")
Debug StringToNumber(@"123bs")
Debug StringToNumber(@"123")
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Post Reply