Jump to content

Working with $strings


Recommended Posts

I have a $string like "blah blah << some - words >> blah blah"

I want to be able to keep only what is between the << >> 's like "some - words".

I have read through all the string functions, but still not sure which one to use, or maybe I need to use a combination of them?

Is anyone familiar with doing this?

Thanks,

RiCK

Link to comment
Share on other sites

an example

#include <string.au3>
$String = "words words words <<This is what I really want>> more words"

$aResults = _StringBetween($String,"<<",">>")
If NOT @error then 
    Msgbox(0,"",$aResults[0])
EndIf
Link to comment
Share on other sites

I just checked inside my <string.au3> file, and that function isn't even in there.

I think it might be cuz I am using an older version of autoit,

is it possible that someone could paste me this function,

and if I paste it into the <string.au3> it will still work for me?

Thanks again,

RiCK

Link to comment
Share on other sites

Get it straight from here...

http://www.autoitscript.com/forum/index.ph...st&p=139139

...but paste it into your script, not the include file.

just my 2 cents

Edit: the version in the current string.au3 include has changed considerably - see the posts below

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Well I did upgrade to the latest version, but then all my old scripts would no longer work properly,

so rather than rewrite all my old scripts, I have just installed the older version again.

If anyone knows some way around that, I'm open to suggestions.

~RiCK

Link to comment
Share on other sites

Thanks again herewasplato, you have been very helpful again!

maybe not so helpful - it has changed a lot since that post and I did not think to test it before posting that link - here it is from the current string include:

;==================================================================================================
; Function Name:        _StringBetween($sString, $sStart, $sEnd, $vCase, $iSRE)
;
; Parameters:           $sString:     The string to search
;                       $sStart:      The beginning of the string to find
;                       $sEnd:        The end of the string to find
;                       $vCase:       Case sensitive search:  Default or -1 = Not case sensitive
;                       $iSRE:        Choose whether to use StringRegExp or Regular Sting Manipulation to get the result
;                                     Default or -1:  Regular String Manipulation used (Non StringRegExp())
;
; Description:          Returns the string between the start search ($sStart) and the end search ($sEnd)
;
; Requirement(s)        AuotIt Beta 3.2.1.8 or higher
;
; Return Value(s)       On Success:    A 0 based array [0] contains the first found string
;                       On Failure:    @Error = 1: No inbetween string was found
;
; Author(s):            SmOke_N
;                       Thanks to Valik for helping with the new StringRegExp (?s)(?i) isssue
;==================================================================================================

Func _StringBetween($sString, $sStart, $sEnd, $vCase = -1, $iSRE = -1)
 If $iSRE = -1 Or $iSRE = Default Then
  If $vCase = -1 Or $vCase = Default Then
   $vCase = 0
  Else
   $vCase = 1
  EndIf
  Local $sHold = '', $sSnSStart = '', $sSnSEnd = ''
  While StringLen($sString) > 0
   $sSnSStart = StringInStr($sString, $sStart, $vCase)
   If Not $sSnSStart Then ExitLoop
   $sString = StringTrimLeft($sString, ($sSnSStart + StringLen($sStart)) - 1)
   $sSnSEnd = StringInStr($sString, $sEnd, $vCase)
   If Not $sSnSEnd Then ExitLoop
   $sHold &= StringLeft($sString, $sSnSEnd - 1) & Chr(1)
   $sString = StringTrimLeft($sString, $sSnSEnd)
  WEnd
  If Not $sHold Then Return SetError(1, 0, 0)
  $sHold = StringSplit(StringTrimRight($sHold, 1), Chr(1))
  Local $avArray[UBound($sHold) - 1]
  For $iCC = 1 To UBound($sHold) - 1
   $avArray[$iCC - 1] = $sHold[$iCC]
  Next
  Return $avArray
 Else
  If $vCase = Default Or $vCase = -1 Then
   $vCase = '(?i)'
  Else
   $vCase = ''
  EndIf
  Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
  If IsArray($aArray) Then Return $aArray
  Return SetError(1, 0, 0)
 EndIf
EndFunc   ;==>_StringBetween

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

I'm using this older function of _StringBetween():

Func _StringBetween($str, $start, $end)
    $pos = StringInStr($str, $start)
    If Not @error Then
        $str = StringTrimLeft($str, $pos + StringLen($start) - 1)
        $pos = StringInStr($str, $end)
        If Not @error Then
            $str = StringTrimRight($str, StringLen($str) - $pos + 1)
            Return $str
        EndIf
    EndIf
EndFuncoÝ÷ Øly鬶(®HhÁëÞ¬ayjgX§+wöÊÞ²ém³
èÞræ¬zØ^~*ì¶Ø¦x[azb zÖ©ä²Úâ
+¶©­ëâ¢ë+zË¥¶Ì¶Ø^±ç(ÛbâjYb´¶Ø^¦º)zºÞ²ém±«­¢+ÙÕ¹AÉÍEÕÉåIÍÕ±ÑÌ ¤($ÀÌØí9ÝMÑÉ¥¹ô}MÑÉ¥¹    Ñݸ ÀÌØíQáаÅÕ½Ðì±Ðì±ÐìÅÕ½Ðì°ÅÕ½ÐìÐìÐìÅÕ½Ðì¤(%%9=PÉɽÈÑ¡¸($%5ͽà À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØí9ÝMÑÉ¥¹¤(%¹%)¹Õ¹oÝ÷ Ø¥²kçb¶X¤zØb±«­¢+ÙÕ¹AÉÍEÕÉåIÍÕ±ÑÌ ¤($ÀÌØí9ÝMÑÉ¥¹ô}MÑÉ¥¹   Ñݸ ÀÌØíQáаÅÕ½Ðì±Ðì±ÐìÅÕ½Ðì°ÅÕ½ÐìÐìÐìÅÕ½Ðì¤(%%9=PÉɽÈÑ¡¸($%5ͽà À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØí9ÝMÑÉ¥¹lÁt¤(%¹%)¹Õ¹oÝ÷ Ø   Ý)¶¬jëh×6Func ParseQueryResults()
    $NewString = _StringBetween($Text,"<<",">>")
    If NOT @error then
        Msgbox(0,"",$NewString[1])
    EndIf
EndFunc

But those last two both produce errors, making the script exit. : ==> Subscript used with non-Array variable.:

What am I doing wrong here?

I just want 1 string returned, I don't even need an array.

Thanks for anyone that can offer some suggestions,

RiCK

Link to comment
Share on other sites

I'm using this older function of _StringBetween():

Func ParseQueryResults()
    $NewString = _StringBetween($Text,"<<",">>")
    If NOT @error then
        Msgbox(0,"",$NewString[1])
    EndIf
EndFunc

But those last two both produce errors, making the script exit. : ==> Subscript used with non-Array variable.:

What am I doing wrong here?

I just want 1 string returned, I don't even need an array.

Thanks for anyone that can offer some suggestions,

RiCK

Try the message box part like this instead,

If NOT @error then
        Msgbox(0,"",$NewString)
    EndIf

Remove the array part.

Edited by Firestorm

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

...the first time I call them I get a blank string or the previous results, yet the second time I call it I get the proper results....

Without seeing the code that you used to "call" this version of the UDF, I'm going to guess that it is the way that the UDF is written:

If it finds nothing, it returns nothing.

If it finds something, it returns something.

If it finds nothing a second time, it returns nothing...

...so your variable is still holding the value that it had when something was found

...but that is just a guess - I've never used either version in one of my scripts, so I don't know them all that well.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Well I did upgrade to the latest version, but then all my old scripts would no longer work properly,

so rather than rewrite all my old scripts, I have just installed the older version again.

If anyone knows some way around that, I'm open to suggestions.

~RiCK

To be honest you shoud upgrade and work out which parts of your old scripts need modifying. The functionality is better in the new release/beta versions I put it off for a little while but I'm glad that I took the time to update my old required scripts. If you run your old scripts in Scite you'll see which lines fail and then you can check out the help file for the new syntax.

You seem to be having so much trouble with this and the code example I posted on page 1 works out of the box with 3.2.10

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