VB.NET remove specific chars between two characters in a string - string

In vb.net how do i remove a character from string which occurs between two known characters in a series.For example how do you remove commas from the number occurring between the hashtag
Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End

You can use this simple and efficient approach using a loop and a StringBuilder:
Dim text = "Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End"
Dim textBuilder As New StringBuilder()
Dim inHashTag As Boolean = False
For Each c As Char In text
If c = "#"c Then inHashTag = Not inHashTag ' reverse Boolean
If Not inHashTag OrElse c <> ","c Then
textBuilder.Append(c) ' append if we aren't in hashtags or the char is not a comma
End If
Next
text = textBuilder.ToString()

Because I'm bad at regex:
Dim str = "Balance,#163,464.24#,Cashbook Closing Balance:,#86,689.45#,Money,End"
Dim split = str.Split("#"c)
If UBound(split) > 1 Then
For i = 1 To UBound(split) Step 2
split(i) = split(i).Replace(",", "")
Next
End If
str = String.Join("#", split)

Related

vb.net check if word exists in string and act accordingly

I read a text file, remove all punctuations and than read all the words in a String(). I want to count the words so I need some String() with two fields, word and frequency. Before I add a word I count the amount of times it is occuring in the text with the Function CountMyWords. If the word already is in the String() I dont want to add it again, just increase it's frequency.
Private Sub CreateWordList()
Dim text As String = File.ReadAllText("C:\Users\Gebruiker\Downloads\shakespear.txt")
text = Regex.Replace(text, "[^A-Za-z']+", " ")
Dim words As String() = text.Split(New Char() {" "c})
Dim i As Integer
For Each word As String In words
If Len(word) > 5 Then
word = word.ToLower()
'now check if the word already exists
If words.Contains(word) = True Then
End If
i = CountMyWords(text, word)
Console.WriteLine("{0}", word + " " + i.ToString)
End If
Next
End Sub
Private Function CountMyWords(input As String, phrase As String) As Integer
Dim Occurrences As Integer = 0
Dim intCursor As Integer = 0
Do Until intCursor >= input.Length
Dim strCheckThisString As String = Mid(LCase(input), intCursor + 1, (Len(input) - intCursor))
Dim intPlaceOfPhrase As Integer = InStr(strCheckThisString, phrase)
If intPlaceOfPhrase > 0 Then
Occurrences += 1
intCursor += (intPlaceOfPhrase + Len(phrase) - 1)
Else
intCursor = input.Length
End If
Loop
CountMyWords = Occurrences
End Function
Any thought how to do that?
As with Steve's answer, I suggest using a Dictionary, but you might not need the overhead of having a class as the value in the dictionary.
Also, if you're using fairly large files, you can process them one line at a time with the File.ReadLines method instead of reading the whole lot into RAM.
You can make the processing of the text a little terser with some LINQ, like this:
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' using https://raw.githubusercontent.com/brunoklein99/deep-learning-notes/master/shakespeare.txt
Dim src = "C:\temp\TheSonnets.txt"
Dim wordsWithCounts As New Dictionary(Of String, Integer)
For Each line In File.ReadLines(src)
Dim text = Regex.Replace(line, "[^A-Za-z']+", " ")
Dim words = text.Split({" "c}).
Where(Function(s) s.Length > 5).
Select(Function(t) t.ToLower())
For Each w In words
If wordsWithCounts.ContainsKey(w) Then
wordsWithCounts(w) += 1
Else
wordsWithCounts.Add(w, 1)
End If
Next
Next
' extracting some data as an example...
Dim mostUsedFirst = wordsWithCounts.
Where(Function(x) x.Value > 18).
OrderByDescending(Function(y) y.Value)
For Each w As KeyValuePair(Of String, Integer) In mostUsedFirst
Console.WriteLine(w.Key & " " & w.Value)
Next
Console.ReadLine()
End Sub
End Module
With the example text, this outputs:
beauty 52
should 44
though 33
praise 28
love's 26
nothing 19
better 19
I would use a different approach. The first thing to do is to create a class that represent the word frequency. It is just a string for the word and an integer to count the word repetitions
Public Class WordFrequency
Public Property Word As String
Public Property Frequency As Integer
End Class
Now, you can create a dictionary where the key is the word and the value is an instance of the WordFrequency class. Using a dictionary is a great bonus in searching if an item exists in the collection. You use a syntax similar to the one used for arrays and specific methods exist to find the element in the collection. So your code becomes simply
' Declared at the global class level
Dim wordCounter As Dictionary(Of String, WordFrequency) = New Dictionary(Of String, WordFrequency)
.....
Private Sub CreateWordList()
Dim text As String = File.ReadAllText("C:\Users\Gebruiker\Downloads\shakespear.txt")
text = Regex.Replace(text, "[^A-Za-z']+", " ")
' remove any blank entries eventually created by the replace
Dim words As String() = text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
For Each word As String In words
If word.Length > 5 Then
word = word.ToLower()
' If we don't have the word in the dictionary, create the entry
If Not wordCounter.ContainsKey(word) Then
wordCounter.Add(word, New WordFrequency With
{
.Word = word,
.Frequency = 0
})
End If
' just increment the property frequency from the dictionary Value
wordCounter(Word).Frequency += 1
End If
Next
End Sub
Note that instead of having a Value of type WordFrequency you can just use an integer for the frequency, but I prefer to have a class because if you ever need to expand the informations kept in the dictionary a class will be easily extended
I suggest a Dictionary:
Public Function CountWords(words As IEnumerable(Of String)) As Dictionary(Of String, Integer)
Dim result As New Dictionary(Of String, Integer)()
For Each word As String In words
If result.ContainsKey(word)
result(word)+=1
Else
result.Add(word, 1)
End If
Next
Return result
End Function
Private Sub CreateWordList(filePath As String)
Dim text As String = File.ReadAllText(filePath).ToLower()
text = Regex.Replace(text, "[^a-z']+", " ")
Dim words As IEnumerable(Of String) = text.Split(New Char() {" "c}).
Where(Function(w) w.Length > 5)
Dim wordCounts As Dictionary(Of String, Integer) = CountMyWords(words)
For Each kvp As KeyValuePair(Of String, Integer) In wordCounts
Console.WriteLine($"{kvp.Key} {kvp.Value}")
Next
End Sub

Issues stripping special characters from text in VBA

I have an Excel file that pulls in data from a csv, manipulates it a bit, and then saves it down as a series of text files.
There are some special characters in the source data that trip things up so I added this to strip them out
Const SpecialCharacters As String = "!,#,#,$,%,^,&,*,(,),{,[,],},?,â,€,™"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String
Dim char As Variant
newString = myString
For Each char In Split(SpecialCharacters, ",")
newString = Replace(newString, char, "")
Next
ReplaceSpecialCharacters = newString
End Function
The issue is that this doesn't catch all of them. When I try to process the following text it slips through the above code and causes Excel to error out.
Hero’s Village
I think the issue is that the special character isn't being recognized by Excel itself. I was only able to get the text to look like it does above by copying it out of Excel and pasting it into a different IDE. In Excel is displays as:
In the workbook
In the edit field
In the immediate window
Based on this site it looks like it's having issues displaying the ' character, but how do I get it to fix/filter it out if it can't even read it properly in VBA itself?
Option Explicit
dim mystring as String
dim regex as new RegExp
Private Function rgclean(ByVal mystring As String) As String
'function that find and replace string if contains regex pattern
'returns str
With regex
.Global = True
.Pattern = "[^ \w]" 'regex pattern will ignore spaces, word and number characters...
End With
rgclean = regex.Replace(mystring, "") '.. and replaces everything else with ""
End Function
Try using regular expression.
Make sure you enable regular expression on:
Tools > References > checkbox: "Microsoft VBScript Regular Expressions 5.5"
Pass the "mystring" string variable into the function (rgclean). The function will check for anything that is not space, word[A-Za-z], or numbers[0-9], replace them with "", and returns the string.
The function will pretty much remove any symbols in the string. Any Numbers, Space, or Word will NOT be excluded.
Here is the opposite approach. Remove ALL characters that are not included in this group of 62:
ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
The code:
Const ValidCharacters As String = "ABCDEFGHIJKLMNOPQESTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Function ReplaceSpecialCharacters(myString As String) As String
Dim newString As String, L As Long, i As Long
Dim char As Variant
newString = myString
L = Len(newString)
For i = 1 To L
char = Mid(newString, i, 1)
If InStr(ValidCharacters, char) = 0 Then
newString = Replace(newString, char, "#")
End If
Next i
ReplaceSpecialCharacters = Replace(newString, "#", "")
End Function
Note:
You can also add characters to the string ValidCharacters if you want to retain them.

Returning the numbers in a string as a variable [duplicate]

I need to find numbers from a string. How does one find numbers from a string in VBA Excel?
Assuming you mean you want the non-numbers stripped out, you should be able to use something like:
Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = retval + Mid(s, i, 1)
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function
Calling this with:
Dim myStr as String
myStr = onlyDigits ("3d1fgd4g1dg5d9gdg")
MsgBox (myStr)
will give you a dialog box containing:
314159
and those first two lines show how you can store it into an arbitrary string variable, to do with as you wish.
Regular expressions are built to parse. While the syntax can take a while to pick up on this approach is very efficient, and is very flexible for handling more complex string extractions/replacements
Sub Tester()
MsgBox CleanString("3d1fgd4g1dg5d9gdg")
End Sub
Function CleanString(strIn As String) As String
Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = "[^\d]+"
CleanString = .Replace(strIn, vbNullString)
End With
End Function
Expanding on brettdj's answer, in order to parse disjoint embedded digits into separate numbers:
Sub TestNumList()
Dim NumList As Variant 'Array
NumList = GetNums("34d1fgd43g1 dg5d999gdg2076")
Dim i As Integer
For i = LBound(NumList) To UBound(NumList)
MsgBox i + 1 & ": " & NumList(i)
Next i
End Sub
Function GetNums(ByVal strIn As String) As Variant 'Array of numeric strings
Dim RegExpObj As Object
Dim NumStr As String
Set RegExpObj = CreateObject("vbscript.regexp")
With RegExpObj
.Global = True
.Pattern = "[^\d]+"
NumStr = .Replace(strIn, " ")
End With
GetNums = Split(Trim(NumStr), " ")
End Function
Use the built-in VBA function Val, if the numbers are at the front end of the string:
Dim str as String
Dim lng as Long
str = "1 149 xyz"
lng = Val(str)
lng = 1149
Val Function, on MSDN
I was looking for the answer of the same question but for a while I found my own solution and I wanted to share it for other people who will need those codes in the future. Here is another solution without function.
Dim control As Boolean
Dim controlval As String
Dim resultval As String
Dim i as Integer
controlval = "A1B2C3D4"
For i = 1 To Len(controlval)
control = IsNumeric(Mid(controlval, i, 1))
If control = True Then resultval = resultval & Mid(controlval, i, 1)
Next i
resultval = 1234
This a variant of brettdj's & pstraton post.
This will return a true Value and not give you the #NUM! error. And \D is shorthand for anything but digits. The rest is much like the others only with this minor fix.
Function StripChar(Txt As String) As Variant
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = Val(.Replace(Txt, " "))
End With
End Function
This is based on another answer, but is just reformated:
Assuming you mean you want the non-numbers stripped out, you should be able to use something like:
'
' Skips all characters in the input string except digits
'
Function GetDigits(ByVal s As String) As String
Dim char As String
Dim i As Integer
GetDigits = ""
For i = 1 To Len(s)
char = Mid(s, i, 1)
If char >= "0" And char <= "9" Then
GetDigits = GetDigits + char
End If
Next i
End Function
Calling this with:
Dim myStr as String
myStr = GetDigits("3d1fgd4g1dg5d9gdg")
Call MsgBox(myStr)
will give you a dialog box containing:
314159
and those first two lines show how you can store it into an arbitrary string variable, to do with as you wish.
Alternative via Byte Array
If you assign a string to a Byte array you typically get the number equivalents of each character in pairs of the array elements. Use a loop for numeric check via the Like operator and return the joined array as string:
Function Nums(s$)
Dim by() As Byte, i&, ii&
by = s: ReDim tmp(UBound(by)) ' assign string to byte array; prepare temp array
For i = 0 To UBound(by) - 1 Step 2 ' check num value in byte array (0, 2, 4 ... n-1)
If Chr(by(i)) Like "#" Then tmp(ii) = Chr(by(i)): ii = ii + 1
Next i
Nums = Trim(Join(tmp, vbNullString)) ' return string with numbers only
End Function
Example call
Sub testByteApproach()
Dim s$: s = "a12bx99y /\:3,14159" ' [1] define original string
Debug.Print s & " => " & Nums(s) ' [2] display original string and result
End Sub
would display the original string and the result string in the immediate window:
a12bx99y /\:3,14159 => 1299314159
Based on #brettdj's answer using a VBScript regex ojbect with two modifications:
The function handles variants and returns a variant. That is, to take care of a null case; and
Uses explicit object creation, with a reference to the "Microsoft VBScript Regular Expressions 5.5" library
Function GetDigitsInVariant(inputVariant As Variant) As Variant
' Returns:
' Only the digits found in a varaint.
' Examples:
' GetDigitsInVariant(Null) => Null
' GetDigitsInVariant("") => ""
' GetDigitsInVariant(2021-/05-May/-18, Tue) => 20210518
' GetDigitsInVariant(2021-05-18) => 20210518
' Notes:
' If the inputVariant is null, null will be returned.
' If the inputVariant is "", "" will be returned.
' Usage:
' VBA IDE Menu > Tools > References ...
' > "Microsoft VBScript Regular Expressions 5.5" > [OK]
' With an explicit object reference to RegExp we can get intellisense
' and review the object heirarchy with the object browser
' (VBA IDE Menu > View > Object Browser).
Dim regex As VBScript_RegExp_55.RegExp
Set regex = New VBScript_RegExp_55.RegExp
Dim result As Variant
result = Null
If IsNull(inputVariant) Then
result = Null
Else
With regex
.Global = True
.Pattern = "[^\d]+"
result = .Replace(inputVariant, vbNullString)
End With
End If
GetDigitsInVariant = result
End Function
Testing:
Private Sub TestGetDigitsInVariant()
Dim dateVariants As Variant
dateVariants = Array(Null, "", "2021-/05-May/-18, Tue", _
"2021-05-18", "18/05/2021", "3434 ..,sdf,sfd 444")
Dim dateVariant As Variant
For Each dateVariant In dateVariants
Debug.Print dateVariant & ": ", , GetDigitsInVariant(dateVariant)
Next dateVariant
Debug.Print
End Sub
Public Function ExtractChars(strRef$) As String
'Extract characters from a string according to a range of charactors e.g'+.-1234567890'
Dim strA$, e%, strExt$, strCnd$: strExt = "": strCnd = "+.-1234567890"
For e = 1 To Len(strRef): strA = Mid(strRef, e, 1)
If InStr(1, strCnd, strA) > 0 Then strExt = strExt & strA
Next e: ExtractChars = strExt
End Function
In the immediate debug dialog:
? ExtractChars("a-5d31.78K")
-531.78

Get only letters from string vb.net

i want to get only letters from string.
eg.
Lets say the string is this :123abc456d
I want to get: abcd
Looking for something like this but for letters in a string:
Dim mytext As String = "123a123"
Dim myChars() As Char = mytext.ToCharArray()
For Each ch As Char In myChars
If Char.IsDigit(ch) Then
MessageBox.Show(ch)
End If
Next
Thanks
You can do it like this :
Dim mytext As String = "123a123"
Dim RemoveChars As String = "0123456789" 'These are the chars that you want to remove from your mytext string
Dim FinalResult As String
Dim myChars() As Char = mytext.ToCharArray()
For Each ch As Char In myChars
If Not RemoveChars.Contains(ch) Then
FinalResult &= ch
End If
Next
MsgBox(FinalResult)
OR :
Dim mytext As String = "1d23ad123d"
Dim myChars() As Char = mytext.ToCharArray()
Dim FinalResult As String
For Each ch As Char In myChars
If Not Char.IsDigit(ch) Then
FinalResult &= ch
End If
Next
MsgBox(FinalResult)
Both will give you the same result.
Hope that helped you :)
You can use Regex to solve this problem. This regex basically says anything that is not in this class, the class being letters in the alphabet then remove by replacing it with nothing.
Dim mytext As String = "123a123"
Dim Result as String = Regex.Replace(myText, "[^a-zA-Z]", "")
Dim myChars() As Char = Result.ToCharArray()
For Each ch As Char In myChars
If Char.IsDigit(ch) Then
MessageBox.Show(ch)
End If
Next
Make sure you have this at the top of your code Imports System.Text.RegularExpressions
Here is a LINQ one liner:
Debug.Print(String.Concat("123abc456d".Where(AddressOf Char.IsLetter)))
Result: abcd.
Here, .Where(AddressOf Char.IsLetter) treats the string as a list of chars, and only keeps letters in the list. Then, String.Concat re-builds the string out of the char list by concatenating the chars.

How to find numbers from a string?

I need to find numbers from a string. How does one find numbers from a string in VBA Excel?
Assuming you mean you want the non-numbers stripped out, you should be able to use something like:
Function onlyDigits(s As String) As String
' Variables needed (remember to use "option explicit"). '
Dim retval As String ' This is the return string. '
Dim i As Integer ' Counter for character position. '
' Initialise return string to empty '
retval = ""
' For every character in input string, copy digits to '
' return string. '
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
retval = retval + Mid(s, i, 1)
End If
Next
' Then return the return string. '
onlyDigits = retval
End Function
Calling this with:
Dim myStr as String
myStr = onlyDigits ("3d1fgd4g1dg5d9gdg")
MsgBox (myStr)
will give you a dialog box containing:
314159
and those first two lines show how you can store it into an arbitrary string variable, to do with as you wish.
Regular expressions are built to parse. While the syntax can take a while to pick up on this approach is very efficient, and is very flexible for handling more complex string extractions/replacements
Sub Tester()
MsgBox CleanString("3d1fgd4g1dg5d9gdg")
End Sub
Function CleanString(strIn As String) As String
Dim objRegex
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Global = True
.Pattern = "[^\d]+"
CleanString = .Replace(strIn, vbNullString)
End With
End Function
Expanding on brettdj's answer, in order to parse disjoint embedded digits into separate numbers:
Sub TestNumList()
Dim NumList As Variant 'Array
NumList = GetNums("34d1fgd43g1 dg5d999gdg2076")
Dim i As Integer
For i = LBound(NumList) To UBound(NumList)
MsgBox i + 1 & ": " & NumList(i)
Next i
End Sub
Function GetNums(ByVal strIn As String) As Variant 'Array of numeric strings
Dim RegExpObj As Object
Dim NumStr As String
Set RegExpObj = CreateObject("vbscript.regexp")
With RegExpObj
.Global = True
.Pattern = "[^\d]+"
NumStr = .Replace(strIn, " ")
End With
GetNums = Split(Trim(NumStr), " ")
End Function
Use the built-in VBA function Val, if the numbers are at the front end of the string:
Dim str as String
Dim lng as Long
str = "1 149 xyz"
lng = Val(str)
lng = 1149
Val Function, on MSDN
I was looking for the answer of the same question but for a while I found my own solution and I wanted to share it for other people who will need those codes in the future. Here is another solution without function.
Dim control As Boolean
Dim controlval As String
Dim resultval As String
Dim i as Integer
controlval = "A1B2C3D4"
For i = 1 To Len(controlval)
control = IsNumeric(Mid(controlval, i, 1))
If control = True Then resultval = resultval & Mid(controlval, i, 1)
Next i
resultval = 1234
This a variant of brettdj's & pstraton post.
This will return a true Value and not give you the #NUM! error. And \D is shorthand for anything but digits. The rest is much like the others only with this minor fix.
Function StripChar(Txt As String) As Variant
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\D"
StripChar = Val(.Replace(Txt, " "))
End With
End Function
This is based on another answer, but is just reformated:
Assuming you mean you want the non-numbers stripped out, you should be able to use something like:
'
' Skips all characters in the input string except digits
'
Function GetDigits(ByVal s As String) As String
Dim char As String
Dim i As Integer
GetDigits = ""
For i = 1 To Len(s)
char = Mid(s, i, 1)
If char >= "0" And char <= "9" Then
GetDigits = GetDigits + char
End If
Next i
End Function
Calling this with:
Dim myStr as String
myStr = GetDigits("3d1fgd4g1dg5d9gdg")
Call MsgBox(myStr)
will give you a dialog box containing:
314159
and those first two lines show how you can store it into an arbitrary string variable, to do with as you wish.
Alternative via Byte Array
If you assign a string to a Byte array you typically get the number equivalents of each character in pairs of the array elements. Use a loop for numeric check via the Like operator and return the joined array as string:
Function Nums(s$)
Dim by() As Byte, i&, ii&
by = s: ReDim tmp(UBound(by)) ' assign string to byte array; prepare temp array
For i = 0 To UBound(by) - 1 Step 2 ' check num value in byte array (0, 2, 4 ... n-1)
If Chr(by(i)) Like "#" Then tmp(ii) = Chr(by(i)): ii = ii + 1
Next i
Nums = Trim(Join(tmp, vbNullString)) ' return string with numbers only
End Function
Example call
Sub testByteApproach()
Dim s$: s = "a12bx99y /\:3,14159" ' [1] define original string
Debug.Print s & " => " & Nums(s) ' [2] display original string and result
End Sub
would display the original string and the result string in the immediate window:
a12bx99y /\:3,14159 => 1299314159
Based on #brettdj's answer using a VBScript regex ojbect with two modifications:
The function handles variants and returns a variant. That is, to take care of a null case; and
Uses explicit object creation, with a reference to the "Microsoft VBScript Regular Expressions 5.5" library
Function GetDigitsInVariant(inputVariant As Variant) As Variant
' Returns:
' Only the digits found in a varaint.
' Examples:
' GetDigitsInVariant(Null) => Null
' GetDigitsInVariant("") => ""
' GetDigitsInVariant(2021-/05-May/-18, Tue) => 20210518
' GetDigitsInVariant(2021-05-18) => 20210518
' Notes:
' If the inputVariant is null, null will be returned.
' If the inputVariant is "", "" will be returned.
' Usage:
' VBA IDE Menu > Tools > References ...
' > "Microsoft VBScript Regular Expressions 5.5" > [OK]
' With an explicit object reference to RegExp we can get intellisense
' and review the object heirarchy with the object browser
' (VBA IDE Menu > View > Object Browser).
Dim regex As VBScript_RegExp_55.RegExp
Set regex = New VBScript_RegExp_55.RegExp
Dim result As Variant
result = Null
If IsNull(inputVariant) Then
result = Null
Else
With regex
.Global = True
.Pattern = "[^\d]+"
result = .Replace(inputVariant, vbNullString)
End With
End If
GetDigitsInVariant = result
End Function
Testing:
Private Sub TestGetDigitsInVariant()
Dim dateVariants As Variant
dateVariants = Array(Null, "", "2021-/05-May/-18, Tue", _
"2021-05-18", "18/05/2021", "3434 ..,sdf,sfd 444")
Dim dateVariant As Variant
For Each dateVariant In dateVariants
Debug.Print dateVariant & ": ", , GetDigitsInVariant(dateVariant)
Next dateVariant
Debug.Print
End Sub
Public Function ExtractChars(strRef$) As String
'Extract characters from a string according to a range of charactors e.g'+.-1234567890'
Dim strA$, e%, strExt$, strCnd$: strExt = "": strCnd = "+.-1234567890"
For e = 1 To Len(strRef): strA = Mid(strRef, e, 1)
If InStr(1, strCnd, strA) > 0 Then strExt = strExt & strA
Next e: ExtractChars = strExt
End Function
In the immediate debug dialog:
? ExtractChars("a-5d31.78K")
-531.78

Resources