F FreeWolF Active Member Licensed User Longtime User Apr 29, 2014 #1 Hello, there is a method for estrapolate an int from a text? For Example: Code: Copy dim a as int dim b as string b = "Hello, I have 3 dogs" I want to set the "dim a" to 3 (the ONLY number in the string). Any ideas? Thanks in advance!
Hello, there is a method for estrapolate an int from a text? For Example: Code: Copy dim a as int dim b as string b = "Hello, I have 3 dogs" I want to set the "dim a" to 3 (the ONLY number in the string). Any ideas? Thanks in advance!
I IlCasti Active Member Licensed User Longtime User Apr 29, 2014 #2 i'm not in front of b4a so try using stringfunctions something like Code: Copy a="" for i = 1 to len(b) if isnumeric(mid(b,i,1)) then a= mid(b,i,1) 'a = a & mid(b,i,1) this for more numbers end if next bye IlCasti Upvote 0
i'm not in front of b4a so try using stringfunctions something like Code: Copy a="" for i = 1 to len(b) if isnumeric(mid(b,i,1)) then a= mid(b,i,1) 'a = a & mid(b,i,1) this for more numbers end if next bye IlCasti
J James Chamblin Active Member Licensed User Longtime User Apr 29, 2014 #3 this works Code: Copy Sub ExtractNumber (a As String) As String Dim result As String Dim digit As Boolean = False Dim decimal As Boolean = False Dim i As Int For i = 0 To a.Length - 1 If "0123456789".Contains(a.CharAt(i)) = True Then 'we found a digit digit = True result = result & a.CharAt(i) Else If digit = True AND a.CharAt(i) = "." AND decimal = False Then 'we found the decimal point decimal = True result = result & a.CharAt(i) Else If digit = True Then'no digit found, end of number Exit End If 'no number has been found yet, go to the next character Next Return result End Sub It will not work properly with negative numbers, -123 will return 123, and any number < 0 must have a leading 0, .123 will return 123, but 0.123 will return 0.123. Upvote 0
this works Code: Copy Sub ExtractNumber (a As String) As String Dim result As String Dim digit As Boolean = False Dim decimal As Boolean = False Dim i As Int For i = 0 To a.Length - 1 If "0123456789".Contains(a.CharAt(i)) = True Then 'we found a digit digit = True result = result & a.CharAt(i) Else If digit = True AND a.CharAt(i) = "." AND decimal = False Then 'we found the decimal point decimal = True result = result & a.CharAt(i) Else If digit = True Then'no digit found, end of number Exit End If 'no number has been found yet, go to the next character Next Return result End Sub It will not work properly with negative numbers, -123 will return 123, and any number < 0 must have a leading 0, .123 will return 123, but 0.123 will return 0.123.
klaus Expert Licensed User Longtime User Apr 29, 2014 #4 This works too: Code: Copy Sub GetNumber(txt As String) As Int Dim c() As String Dim i As Int c = Regex.Split(" ", txt) For i = 0 To c.Length - 1 If IsNumber(c(i)) Then Exit End If Next If i < c.Length Then Return c(i) Else ToastMessageShow("No number", False) End If End Sub Call it with : Code: Copy Dim a As Int a = GetNumber("Hello, I have -1 dogs") It returns only the first number. Upvote 0
This works too: Code: Copy Sub GetNumber(txt As String) As Int Dim c() As String Dim i As Int c = Regex.Split(" ", txt) For i = 0 To c.Length - 1 If IsNumber(c(i)) Then Exit End If Next If i < c.Length Then Return c(i) Else ToastMessageShow("No number", False) End If End Sub Call it with : Code: Copy Dim a As Int a = GetNumber("Hello, I have -1 dogs") It returns only the first number.
LucaMs Expert Licensed User Longtime User Apr 29, 2014 #6 I want to make myself agreeable. The solution of James does not work when the decimal points and commas for the thousands are reversed, as here in Italy. It will be sufficient to use string variables for these characters. The solution of Klaus will not work if the number is next to characters that are not spaces. (I'm a troublemakers, I know ) Upvote 0
I want to make myself agreeable. The solution of James does not work when the decimal points and commas for the thousands are reversed, as here in Italy. It will be sufficient to use string variables for these characters. The solution of Klaus will not work if the number is next to characters that are not spaces. (I'm a troublemakers, I know )
I IlCasti Active Member Licensed User Longtime User Apr 29, 2014 #8 Klaus you cannot have -1 dog. Maybe James can have 0.123 dog (something like a chihuahua) Mine, if it works, is the best one LOL Luca you're right, you are troublemakers Upvote 0
Klaus you cannot have -1 dog. Maybe James can have 0.123 dog (something like a chihuahua) Mine, if it works, is the best one LOL Luca you're right, you are troublemakers
LucaMs Expert Licensed User Longtime User Apr 29, 2014 #9 My solution is... damn, i must change my Avatar, sorry Done. __________________________________________________ My "solution": I do not know Regex (Erel is an expert), but I think we can estropolare groups using it. I leave this to the alien Or, using some variables for comma and dot, run the cycle as IlCasti has done, and using vars like NumStartPos NumEndPos, etc.. Ok, I try to write it ... but I would prefer the intervention of an expert in Regex Last edited: Apr 29, 2014 Upvote 0
My solution is... damn, i must change my Avatar, sorry Done. __________________________________________________ My "solution": I do not know Regex (Erel is an expert), but I think we can estropolare groups using it. I leave this to the alien Or, using some variables for comma and dot, run the cycle as IlCasti has done, and using vars like NumStartPos NumEndPos, etc.. Ok, I try to write it ... but I would prefer the intervention of an expert in Regex
LucaMs Expert Licensed User Longtime User Apr 29, 2014 #10 Code: Copy Private lstNum As List : lstNum.Initialize Private Text As String Text = "FreeWolf has 3 dogs, 2 wolfs --> ,5.5 million dollars but live in a city where the temperature is always between -15°" lstNum = NumsFromString(Text) For I = 0 To lstNum.Size - 1 Log(lstNum.Get(I)) Next Code: Copy Private Sub NumsFromString(Text As String) As List Private lstResult As List : lstResult.Initialize Private ValidChars As String = "-+0123456789.," Private NumStartPos, ValidCharPos As Int Private Ch, Num As String NumStartPos = -1 Num = "" For c = 0 To Text.Length - 1 Ch = Text.SubString2(c, c + 1) ValidCharPos = ValidChars.IndexOf(Ch) If ValidCharPos >= 0 Then If NumStartPos >= 0 Then Num = Num & Ch Else NumStartPos = c Num = Num & Ch End If Else If Num <> "" Then If IsNumber(Num) Then lstResult.Add(Num) End If Num = "" End If NumStartPos = -1 End If Next If Num <> "" Then If IsNumber(Num) Then lstResult.Add(Num) End If End If Return lstResult End Sub Upvote 0
Code: Copy Private lstNum As List : lstNum.Initialize Private Text As String Text = "FreeWolf has 3 dogs, 2 wolfs --> ,5.5 million dollars but live in a city where the temperature is always between -15°" lstNum = NumsFromString(Text) For I = 0 To lstNum.Size - 1 Log(lstNum.Get(I)) Next Code: Copy Private Sub NumsFromString(Text As String) As List Private lstResult As List : lstResult.Initialize Private ValidChars As String = "-+0123456789.," Private NumStartPos, ValidCharPos As Int Private Ch, Num As String NumStartPos = -1 Num = "" For c = 0 To Text.Length - 1 Ch = Text.SubString2(c, c + 1) ValidCharPos = ValidChars.IndexOf(Ch) If ValidCharPos >= 0 Then If NumStartPos >= 0 Then Num = Num & Ch Else NumStartPos = c Num = Num & Ch End If Else If Num <> "" Then If IsNumber(Num) Then lstResult.Add(Num) End If Num = "" End If NumStartPos = -1 End If Next If Num <> "" Then If IsNumber(Num) Then lstResult.Add(Num) End If End If Return lstResult End Sub
F FreeWolF Active Member Licensed User Longtime User Apr 29, 2014 #11 Thanks a lot LucaMS I will try your code too! But I have tried also the other codes and run. But sometimes the value I return is 0.... Upvote 0
Thanks a lot LucaMS I will try your code too! But I have tried also the other codes and run. But sometimes the value I return is 0....
LucaMs Expert Licensed User Longtime User Apr 30, 2014 #12 I tried to write code that is also suitable for other types of sentences. The code that they have proposed works for the phrase that you have proposed. You could also solve giving away the 3 dogs, so the sentence would be: "I do not have dogs" and you would not need functions (Warning: Do not use "I had 3 dogs") Upvote 0
I tried to write code that is also suitable for other types of sentences. The code that they have proposed works for the phrase that you have proposed. You could also solve giving away the 3 dogs, so the sentence would be: "I do not have dogs" and you would not need functions (Warning: Do not use "I had 3 dogs")
MaFu Well-Known Member Licensed User Longtime User Apr 30, 2014 #13 Code: Copy Regex.Split("[+-]?\d+(\.\d+)?", txt) works für following number formats: 123 +123 -123 123.456 +123.456 -123.456 Upvote 0
Code: Copy Regex.Split("[+-]?\d+(\.\d+)?", txt) works für following number formats: 123 +123 -123 123.456 +123.456 -123.456
LucaMs Expert Licensed User Longtime User Apr 30, 2014 #14 MaFu said: Code: Copy Regex.Split("[+-]?\d+(\.\d+)?", txt) works für following number formats: 123 +123 -123 123.456 +123.456 -123.456 Click to expand... I had written: Ok, I try to write it ... but I would prefer the intervention of an expert in Regex You could have avoided a lot of work to me Thanks, Manfred Upvote 0
MaFu said: Code: Copy Regex.Split("[+-]?\d+(\.\d+)?", txt) works für following number formats: 123 +123 -123 123.456 +123.456 -123.456 Click to expand... I had written: Ok, I try to write it ... but I would prefer the intervention of an expert in Regex You could have avoided a lot of work to me Thanks, Manfred
J James Chamblin Active Member Licensed User Longtime User Apr 30, 2014 #15 This makes better use of RegEx Code: Copy Dim a As String = "I have 3 dogs, 1.5 pigs (ate half of one already), and -.75 chickens (one of my dogs ate the neighbors lunch after he only ate a quarter of his chicken, so now I owe him the rest of his lunch)." Dim m As Matcher m = Regex.Matcher("[+-]?(\d+\.?\d*|\.\d+)",a) Do While m.Find Log("Number = " & m.Match) Loop Wrote my response while MaFu was writing his. Upvote 0
This makes better use of RegEx Code: Copy Dim a As String = "I have 3 dogs, 1.5 pigs (ate half of one already), and -.75 chickens (one of my dogs ate the neighbors lunch after he only ate a quarter of his chicken, so now I owe him the rest of his lunch)." Dim m As Matcher m = Regex.Matcher("[+-]?(\d+\.?\d*|\.\d+)",a) Do While m.Find Log("Number = " & m.Match) Loop Wrote my response while MaFu was writing his.
LucaMs Expert Licensed User Longtime User Apr 30, 2014 #16 James Chamblin said: Wrote my response while MaFu was writing his. Click to expand... Anyway, too late. I must anyway collect sweat I ejected. Upvote 0
James Chamblin said: Wrote my response while MaFu was writing his. Click to expand... Anyway, too late. I must anyway collect sweat I ejected.