Jump to content
Sign in to follow this  
JennMaughan

Extracting numbers from a string

Recommended Posts

JennMaughan

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

Share this post


Link to post
Share on other sites
mikell

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

  • Like 2

Share this post


Link to post
Share on other sites
iamtheky

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

 

  • Like 4

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

Share this post


Link to post
Share on other sites
mikell
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

Share this post


Link to post
Share on other sites
iamtheky

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


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

Share this post


Link to post
Share on other sites
kylomas

@iamthekey - As Goldberg'ish as that is I would never have thought of using stringsplit like that.  Bravo!

  • Like 1

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
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
Sign in to follow this  

×