Jump to content

Extracting numbers from a string


Recommended Posts

I'm bridging into a new area of experience in Autoit, and not sure what is the best to use.

------------------------------------------------------------------

I have written a portion of AutoIt script that reads through a notepad file and copies the data between an identified starting phrase and ending phrase, then saves the result in specific Excel cells.

What I need to have happen is to then to go back ... and extract the number that is listed in each string.  There will only be one number such as....     75,689.     

I'm not sure how to do that the easiest

-------------------------------

Overall...   looking at the project globally... All I really need to do is extract the number following a certain phrase in a file ....and Just because I didn't know how to do it better...  I'm running the $aExtract = _StringBetween($sText, $sStart, $sEnd)...  pasting string into Excel... then hoping to just extract the number from that string...      I'm using Notepad just because it seemed easy

If there's a much better way overall... please lol, let me know .. Thx  

 

Thanks!  Jen

Link to comment
Share on other sites

Regular expressions do exactly this  :)
Example :

$str = "What I need to have happen is to then to go back ... and extract the number 75,689 that is listed in each string." 

$nbr = StringRegExp($str, '\d+(?:,\d+)?', 1)
If IsArray($nbr) Then Msgbox(0,"", $nbr[0])

This code can be adapted so you might provide sample strings

Link to comment
Share on other sites

there are soooo many ways to do this.  Understanding why mikell is right is a greater goal than just using his correct solutions.

#include<array.au3>

$str = "What I need to have happen is to then to go back ... and extract the number 75,689 that is listed in each string."

msgbox(0, '' , StringStripWS(_ArrayToString(stringsplit($str , "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ." , 2), ""), 3))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

55 minutes ago, iamtheky said:

Understanding why mikell is right is a greater goal than just using his correct solutions.

Hmm... does this mean that I should have provided some relevant comments to explain the code ? if so you're not wrong  :)
Let's wait first for the OP's feedback, to be sure that it works for all the possibly concerned strings and fits all the requirements (including the hidden ones) etc

Link to comment
Share on other sites

Not necessarily, I think uncommented code forces you to learn just as much as commented code may teach.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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