dickjones007 Posted May 25, 2011 Share Posted May 25, 2011 how can I extract one or multiple numbers from one sentence example sentence (with colon): "Take 5 and add 9:" i basically would like to do that function, extract numbers 5 and 9 and then add them Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 25, 2011 Moderators Share Posted May 25, 2011 dickjones, Assuming that you do not need to parse the text to decide on the operation to carry out it is very easy: $sString = "Take 5 and add 9:" $aArray = StringRegExp($sString, "(?i)(?U).*(\d+).*", 3) ConsoleWrite($aArray[0] & " + " & $aArray[1] & " = " & $aArray[0] + $aArray[1] & @CRLF) If you need to vary the operation depending on the words used, then it gets to a whole new level of fun! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
UEZ Posted May 25, 2011 Share Posted May 25, 2011 Here another method if your numbers have floating point values: $sText = "Take 5 and add 9:" & @CRLF & _ "Take 1.23 and add 12.1234 and add 165:" $aSentences = StringSplit($sText, ":", 2) Global $aNumbers, $sum, $sResult For $i = 0 To UBound($aSentences) - 1 If $aSentences[$i] <> "" Then $aNumbers = StringRegExp($aSentences[$i], "([\d]+\.?\d*)(?:\d)*", 3) For $j = 0 To UBound($aNumbers) - 1 $sum += $aNumbers[$j] Next $sResult &= "Sum of sentence " & $i + 1 & " = " & $sum & @CRLF $sum = 0 EndIf Next MsgBox(0, "Info", $sText & @CRLF & "--------------------------------------------------------------------" & @CRLF & $sResult) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
dickjones007 Posted May 25, 2011 Author Share Posted May 25, 2011 Here another method if your numbers have floating point values: $sText = "Take 5 and add 9:" & @CRLF & _ "Take 1.23 and add 12.1234 and add 165:" $aSentences = StringSplit($sText, ":", 2) Global $aNumbers, $sum, $sResult For $i = 0 To UBound($aSentences) - 1 If $aSentences[$i] <> "" Then $aNumbers = StringRegExp($aSentences[$i], "([\d]+\.?\d*)(?:\d)*", 3) For $j = 0 To UBound($aNumbers) - 1 $sum += $aNumbers[$j] Next $sResult &= "Sum of sentence " & $i + 1 & " = " & $sum & @CRLF $sum = 0 EndIf Next MsgBox(0, "Info", $sText & @CRLF & "--------------------------------------------------------------------" & @CRLF & $sResult) Br, UEZ thats it thank you for this now can this text be taken from, lets say, equation.txt file? and then insert the solution into page text field with html <input class="answer" type="text">? Link to comment Share on other sites More sharing options...
UEZ Posted May 25, 2011 Share Posted May 25, 2011 (edited) I have no knowledge with HTML and I don't know how your real text looks like!Of course you can load a text file. Have a look to FileRead() function.Br,UEZ Edited May 25, 2011 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now