Jump to content

stringinstr not working?


Recommended Posts

Hello, I can't seem to get stringinstr working when I specify a starting position. Any help would be appreciated. I'm running v3.2.12.0.

;this will get the word woodchuck1 from the following
$myString = "How much wood could woodchuck1 chuck if woodchuck2 could chuck wood?"
$start = StringInStr($myString, "woodchuck", 0, 1, 19); start at the end of the 1st "could"
$end = StringInStr($myString, "woodchuck") + 9; end at end of the 1st woodchuck
$myWord = stringmid($myString, $start, $end - $start)
MsgBox(0, "Which woodchuck?", $myWord)
Link to comment
Share on other sites

  • Moderators

I didn't test this much:

;this will get the word woodchuck1 from the following
$myString = "How much wood could woodchuck1 chuck if woodchuck2 could chuck wood?"
$myword = _somefunction($myString, "woodchuck", 0, 1)
MsgBox(0, "Which woodchuck?", $myword)

Func _somefunction($sString, $sFind, $vCase = 0, $iInstance = 1)
    If  $vCase = 0 Or $vCase = Default Then $vCase = "(?i)"
    If $vCase <> "(?i)" Then $vCase = ""
    Local $aSRE = StringRegExp($sString, "(?s)" & $vCase & _
                    ".*?(" & $sFind & ".+?)\W", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    If UBound($aSRE) < $iInstance Then Return SetError(2, 0, "")
    Return $aSRE[$iInstance - 1]
EndFunc

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.

Link to comment
Share on other sites

I didn't test this much:

;this will get the word woodchuck1 from the following
$myString = "How much wood could woodchuck1 chuck if woodchuck2 could chuck wood?"
$myword = _somefunction($myString, "woodchuck", 0, 1)
MsgBox(0, "Which woodchuck?", $myword)

Func _somefunction($sString, $sFind, $vCase = 0, $iInstance = 1)
    If  $vCase = 0 Or $vCase = Default Then $vCase = "(?i)"
    If $vCase <> "(?i)" Then $vCase = ""
    Local $aSRE = StringRegExp($sString, "(?s)" & $vCase & _
                    ".*?(" & $sFind & ".+?)\W", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    If UBound($aSRE) < $iInstance Then Return SetError(2, 0, "")
    Return $aSRE[$iInstance - 1]
EndFunc
I saw SmOke_N lurking, and KNEW there was a RegExp in your future!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

I saw SmOke_N lurking, and KNEW there was a RegExp in your future!

:)

Well of course lol...

Thought I'd post a better example... of how to use it (without comments of course :) )

;this will get the word woodchuck1 from the following
$myString = "How much wood could woodchuck1 chuck if woodchuck2 could chuck wood?"
$myword = _StringGetWordInstance($myString, "woodchuck")
MsgBox(0, "Which woodchuck?", $myword)
$myword2 = _StringGetWordInstance($myString, "woodchuck", 0, 2)
MsgBox(0, "Which woodchuck?", $myword2)

Func _StringGetWordInstance($sString, $sFind, $vCase = 0, $iInstance = 1)
    If  $vCase = 0 Or $vCase = Default Then $vCase = "(?i)"
    If $vCase <> "(?i)" Then $vCase = ""
    Local $aSRE = StringRegExp($sString, "(?s)" & $vCase & _
                    ".*?(" & $sFind & ".+?)\W", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    If UBound($aSRE) < $iInstance Then Return SetError(2, 0, "")
    Return $aSRE[$iInstance - 1]
EndFunc

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.

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