Vikramjeet Posted October 25, 2018 Share Posted October 25, 2018 I am reading a specific line in a log file. I need help to extract the numbers after specific text and then add these numbers. Another option would be to read the last 12 characters of the line, extract only numbers and then add them AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213 From this line I want to extract 12, 20 and 213 and then add them 12+20+213=245 1- I can read the last 12 characters, extract only numbers and then add them 2- I can read the text after 'TIM XON LIST' , extract only numbers and then add them Thank you Link to comment Share on other sites More sharing options...
iamtheky Posted October 25, 2018 Share Posted October 25, 2018 $str = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213" msgbox(0, '' , Execute(StringRegExpReplace(stringmid($str , stringinstr($str , " " , 0 , -1) + 1) , "\D" , "+"))) IAMK and FrancescoDiMuro 2 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 25, 2018 Moderators Share Posted October 25, 2018 (edited) Vikramjeet, Not difficult at all. What have YOU tried that has not worked? M23 P.S. If you get stuck: Spoiler #include <StringConstants.au3> #include <MsgBoxConstants.au3> $sLine = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213 " ; Extract digits from the final 12 characters $aRet = StringRegExp(StringRight($sLine, 12), "(\d+)", $STR_REGEXPARRAYGLOBALMATCH) ; Declare variable to hold total $iTotal = 0 ; Loop through the array to add the values For $i = 0 To UBound($aRet) - 1 $iTotal += $aRet[$i] Next ; And here it is MsgBox($MB_SYSTEMMODAL, "Total", $iTotal) Edit: iamtheky - nice! Edited October 25, 2018 by Melba23 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...
FrancescoDiMuro Posted October 25, 2018 Share Posted October 25, 2018 @iamtheky Global $strString = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213", _ $arrResult, _ $strNumbers = "", _ $fltSum = 0.0 $arrResult = StringRegExp($strString, ".{1}(\d{2}).{1}(\d{2}).{1}(\d{3})", 1) For $i = 0 To UBound($arrResult) - 1 Step 1 If $i < UBound($arrResult) - 1 Then $strNumbers &= $arrResult[$i] & "+" Else $strNumbers &= $arrResult[$i] EndIf $fltSum += $arrResult[$i] Next ConsoleWrite("Sum of " & $strNumbers & " = " & $fltSum & @CRLF) What do you think about? Please, let me know Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Vikramjeet Posted October 25, 2018 Author Share Posted October 25, 2018 33 minutes ago, Melba23 said: Vikramjeet, Not difficult at all. What have YOU tried that has not worked? M23 P.S. If you get stuck: Reveal hidden contents #include <StringConstants.au3> #include <MsgBoxConstants.au3> $sLine = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213 " ; Extract digits from the final 12 characters $aRet = StringRegExp(StringRight($sLine, 12), "(\d+)", $STR_REGEXPARRAYGLOBALMATCH) ; Declare variable to hold total $iTotal = 0 ; Loop through the array to add the values For $i = 0 To UBound($aRet) - 1 $iTotal += $aRet[$i] Next ; And here it is MsgBox($MB_SYSTEMMODAL, "Total", $iTotal) Edit: iamtheky - nice! I was able to extract ALL numbers from the string. However i was not able to extract the specific numbers Link to comment Share on other sites More sharing options...
Vikramjeet Posted October 25, 2018 Author Share Posted October 25, 2018 3 hours ago, iamtheky said: $str = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213" msgbox(0, '' , Execute(StringRegExpReplace(stringmid($str , stringinstr($str , " " , 0 , -1) + 1) , "\D" , "+"))) Many thanks iamtheky Link to comment Share on other sites More sharing options...
Vikramjeet Posted October 25, 2018 Author Share Posted October 25, 2018 3 hours ago, FrancescoDiMuro said: @iamtheky Global $strString = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213", _ $arrResult, _ $strNumbers = "", _ $fltSum = 0.0 $arrResult = StringRegExp($strString, ".{1}(\d{2}).{1}(\d{2}).{1}(\d{3})", 1) For $i = 0 To UBound($arrResult) - 1 Step 1 If $i < UBound($arrResult) - 1 Then $strNumbers &= $arrResult[$i] & "+" Else $strNumbers &= $arrResult[$i] EndIf $fltSum += $arrResult[$i] Next ConsoleWrite("Sum of " & $strNumbers & " = " & $fltSum & @CRLF) What do you think about? Please, let me know Thank you so much Francesco Link to comment Share on other sites More sharing options...
Malkey Posted October 26, 2018 Share Posted October 26, 2018 Another method. Local $str = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213" Local $sRet = StringRegExpReplace($str, ".+[A-Z](\d+)[A-Z](\d+)[A-Z](\d+)$", "\1 + \2 + \3") ; The RE pattern is anchored to the end of the string, "$". MsgBox(0, 'Results', $sRet & " = " & Execute($sRet)) ; Returns:- ; 12 + 20 + 213 = 245 Link to comment Share on other sites More sharing options...
gruntydatsun Posted October 26, 2018 Share Posted October 26, 2018 looks like you're spoiled for choice today Vikramjeet Local $string = "AX 123 26OCT ABC 23 1800 UHS TIM XON LIST A12B20C213" Local $sum = 0 $array = StringRegExp($string,"A(\d*?)B(\d*?)C(\d*?)$",3) for $element in $array $sum += $element Next msgbox(1,"SUM",$sum) 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