Jump to content

extract numbers from sentence


Recommended Posts

  • Moderators

dickjones,

Assuming that you do not need to parse the text to decide on the operation to carry out it is very easy: :huh2:

$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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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

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 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...