Jump to content

Recommended Posts

Posted

How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function?

Some string examples and the position of the first numeric within the string:

'1acde ffga 12zmo r' the result would be 1

'abcdef 012 zuf9gas' the result would be 8

'ab7cdef 973 zufsgas' the result would be 3

'abcdefgh ijkl mnop 7' the result would be 20

Assistance is always appreciated Ant..

Posted (edited)

How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function?

Some string examples and the position of the first numeric within the string:

'1acde ffga 12zmo r' the result would be 1

'abcdef 012 zuf9gas' the result would be 8

'ab7cdef 973 zufsgas' the result would be 3

'abcdefgh ijkl mnop 7' the result would be 20

Assistance is always appreciated Ant..

With the StringRegExp() function. or StringInStr()

An example with StringInStr() function :

$String = "fndjsk123"
For $i = 0 To 9
    $Position = StringInStr($String,$i)
    If $Find <> 0 Then
        MsgBox(0,"",$Position)
        Exit
    EndIf
Next
Edited by jerem488

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Posted

With the StringRegExp() function. or StringInStr()

An example with StringInStr() function :

$String = "fndjsk123"
For $i = 0 To 9
    $Position = StringInStr($String,$i)
    If $Find <> 0 Then
        MsgBox(0,"",$Position)
        Exit
    EndIf
Next
Thanks for that given that the string can vari in length I have modified your example as follows

CODE
$String = "fndjskaaaa0123"

For $i = 0 To stringlen($string)

$Position = StringInStr($String,$i)

If $position <> 0 Then

MsgBox(0,"",$Position)

Exit

EndIf

Next

Posted (edited)

No...

He goes from 0 to 9 because they are the only digits to check... StringInStr returns the position of the chosen string or 0. You DO NOT need to do it like you are now. Read his example again and try understand exactly whats happening.

Cheers,

Brett

EDIT:

But, the example might have flaws (I haven't tested it) as it might check for 0, yet 0 is the 2nd digit etc. Let me whip something different up :)

EDIT:

This works correctly for me :)

$string = "abcdefg1f0d93045"
MsgBox(0, "", _CheckStringFirstDigit($string))

Func _CheckStringFirstDigit($string)
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
    Return $i
EndFunc  ;==>_CheckStringFirstDigit
Edited by BrettF
Posted

If you do care about speed... you ought to drop the "<> 0" as demonstrated here:

$String = "fndjs4gffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position <> 0 Then
;           MsgBox(0,"","Position: " & $Position)
            ExitLoop
        EndIf
    Next
Next

Msgbox(1,"", int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then
;           MsgBox(0,"","Position: " & $Position)
            ExitLoop
        EndIf
    Next
Next
Msgbox(1,"", int(TimerDiff($time)))
Posted

I just put jerem488's example in a 10000-iteration loop with a timer to demonstrate that although it works fine as is, it works even better without the "<> 0". Just trying to encourage efficient coding.

Posted

Ouch.

$String = "0fndjs4gffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(1,"", int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
Next
Msgbox(1,"", int(TimerDiff($time)))
Posted (edited)

That one's FAST, Auth! Do that one OP!

Edit: babbling... (and here I generally considered StringRegExp a dog, I guess I seem to relate it to Grep and EGrep)

Edited by Spiff59
Posted

:)

$String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"
Dim $Dummy, $Position   

$time = TimerInit()

For $z = 1 to 1000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(0x40,'StringInStr', int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 1000
    $Dummy = StringRegExp($String, '\d', 1)
    $Position = @extended-1
Next

Don't add unneeded code lol, now ouch!... XP

Posted

Ouch.

Yes it may be faster but it returns the wrong result:

See the following:

$String = "fndjs4g0ffgdfb66"

$time = TimerInit()

For $z = 1 to 10000
    For $i = 0 To 9
        $Position = StringInStr($String,$i)
        If $Position Then ExitLoop
    Next
Next

Msgbox(0,$Position, int(TimerDiff($time)))

$time = TimerInit()
For $z = 1 to 10000
    $split = StringSplit($string, "")
    For $i = 1 To $split[0]
        If StringRegExp($split[$i], "\d") Then ExitLoop
    Next
Next
Msgbox(0,$i, int(TimerDiff($time)))

Just go with what Auth posted. It confused me because there is no mention of @extended return the position...

Cheers,

Brett

Posted

Yes, I concede!

I'll stop going "ewww" everytime I see SRE.

I'll quit letting it remind me of UNIX scripting 20 years ago, the details of which I've long forgotten, but for which the unpleasant aftertaste still lingers :)

Posted (edited)

how about

$String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"

Dim $Dummy, $Position   

$time = TimerInit()

$ss=StringSplit($String,"")
    For $i = 1 To $ss[0]
    if StringIsDigit($ss[$i])=1 then ExitLoop
Next
Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])
Edited by Aceguy
Posted (edited)

$String = "1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
          "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa"

Dim $Dummy, $Position   

$time = TimerInit()

For $z = 0 To 500
    $ss=StringSplit($String,"")
        For $i = 1 To $ss[0]
        if StringIsDigit($ss[$i]) then ExitLoop
    Next
Next

Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])


$time = TimerInit()

For $z = 0 To 500
    $Dummy = StringRegExp($String, '\d')
    $Position = $Dummy
Next

Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i])

I added 1 in front of the string, still, the results are crystal clear. (=P

...@extended-1 instead of $Dummy.

Edited by Authenticity
Posted

Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows.

;//Extract the Phone Number

$sString = "Most Urgent 61390890965"

StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance

$sPosition = @extended - 1 ;calculate the @mid start position

$sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1))

Msgbox(0,"",$sPhoneNum,5)

Thanks again Ant..

  • Moderators
Posted

Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows.

;//Extract the Phone Number

$sString = "Most Urgent 61390890965"

StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance

$sPosition = @extended - 1 ;calculate the @mid start position

$sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1))

Msgbox(0,"",$sPhoneNum,5)

Thanks again Ant..

Hmm...
Local $a_phone = StringRegExp($s_string, "\d+", 1)
If Not @error Then MsgBox(64, "Number", $a_phone[0])

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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
×
×
  • Create New...