Jump to content

Strings with numbers


Recommended Posts

Hi,

I am having trouble with numbers in strings. I need to read each line and capture just the numbers, some have a decimal points (with no more than two digits after the decimal). Each line has just one number (and may be like 54 or 162 or 1.2 or 10.54) and some text.

Any help is appreciated.

Thanks

Link to comment
Share on other sites

Here a method with RegEx:

#include <Array.au3>
$string = "Line 1 with 12.55 values" & @LF & _
                "Line 2 with 100 digits" & @LF & _
                "2011 is the year of the 10.2 winners"
$aNumbers = StringRegExp($string, "[\d.]+", 3)
_ArrayDisplay($aNumbers, "Filtering numbers off the string")

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

Dangit UEZ...I was working on a n00b style version...not because he is a n00b, but because I am...haha...anyway...here is another way that it could be done.

$String = 'Alpha123.5'
$NumString = ''

$array = StringSplit($String,'')

For $i = 1 to $array[0]
    If StringisDigit($array[$i]) Then
        $NumString &= $array[$i]
    Else
        If StringinStr($array[$i],'.') Then
        $NumString &= $array[$i]
        EndIf
    EndIf
Next
        
MsgBox(0,'',$NumString)
Link to comment
Share on other sites

Your way dufran3 is close to what I came up with. I'm pretty novice at this too.. :unsure:

#include <String.au3>
#include <Array.au3>

$teststring = "this is 1 line not 25 lines of text."

$teststringarray = _StringExplode($teststring, " ", 0)
;_ArrayDisplay($teststringarray, "StringExplode 0")

For $a = 0 To UBound($teststringarray) - 1
    If Number($teststringarray[$a]) > 0 Then ConsoleWrite($teststringarray[$a] & " is a number." & @LF)
Next

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Link to comment
Share on other sites

and another, im certain flawed, method.

$string = "sadf14.01dkhfasjk"
;~ $string = "asdkjfa6785asdgka"
;~ $string = "fghdkfh78dfkgksdjkalf"

$reg = stringregexp($string , "(\d+\.?\d?\d?)" , 1)

msgbox(0, '' , $reg[0])

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

Link to comment
Share on other sites

Using a modified part of the string from UEZ

$sStr = "2011 is the year of the 10.234 winners"
$aNums = StringRegExp($sStr, "([\d]+\.?\d{0,2})(?:\d)*", 3);  Create a 0 based array of the numbers in the string to a maximum of 2 decimal places
If NOT @Error Then
   ;; do what you want with those numbers in here
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

and another, im certain flawed, method.

$string = "sadf14.01dkhfasjk"
;~ $string = "asdkjfa6785asdgka"
;~ $string = "fghdkfh78dfkgksdjkalf"

$reg = stringregexp($string , "(\d+\.?\d?\d?)" , 1)

msgbox(0, '' , $reg[0])

Not really flawed. The only problem I see is in the event of there being more than 1 matching number in a string it would only get the first because of the 1 flag.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Most decent threads will have something in them that almost everyone can learn from.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Well, I'm also a RegEx novice and I'm trying to keep it as simple as possible.

Such kind of threads are good to exercise on RegEx and learn from the pros. :unsure:

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

And you were very close. Actually it would have worked fine except for the part where he wanted it resticted to 2 decimal points. In fact the SRE you used would be good if he was willing to handle the decimal places at a later stage using Round().

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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