Jump to content

_StrBetween()


SmOke_N
 Share

Recommended Posts

  • Moderators

This gets asked for more and more...

This will give you the option of using StringRegExp() or regular string manipulation to get the desired result.

The return is a 0 based array for either or.

Example:

#include <array.au3>
$a = _StrBetween('[18][20][3][5][500][60]', '[', ']');Not using SRE
_ArrayDisplay($a, '')
$b = _StrBetween('[18][20][3][5][500][60]', '\[', '\]', -1, 1);Using SRE
_ArrayDisplay($b, '')oÝ÷ Ù«­¢+Øìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ìչѥ½¸9µè}MÑÉ  Ñݸ ÀÌØíÍMÑÉ¥¹°ÀÌØíÍMÑÉаÀÌØí͹°ÀÌØíÙ
Í°ÀÌØí¥MI¤(ì(ìAɵÑÉÌèÀÌØíÍMÑÉ¥¹èQ¡ÍÑÉ¥¹Ñ¼ÍÉ (ìÀÌØíÍMÑÉÐèQ¡¥¹¹¥¹½Ñ¡ÍÑÉ¥¹Ñ¼¥¹(ìÀÌØí͹èQ¡¹½Ñ¡ÍÑÉ¥¹Ñ¼¥¹(ìÀÌØíÙ
Íè
Í͹ͥѥÙÍÉ èձнȴÄô9½ÐÍ͹ͥѥÙ(ìÀÌØí¥MIè
¡½½ÍÝ¡Ñ¡ÈѼÕÍMÑÉ¥¹IáÀ½ÈIÕ±ÈMÑ¥¹5¹¥Áձѥ½¸Ñ¼ÐÑ¡ÉÍÕ±Ð(ìձнȴÄèIÕ±ÈMÑÉ¥¹5¹¥Áձѥ½¸ÕÍ¡9½¸MÑÉ¥¹IáÀ ¤¤(ì(ìÍÉ¥ÁÑ¥½¸èIÑÕɹÌÑ¡ÍÑÉ¥¹ÑݸѡÍÑÉÐÍÉ  ÀÌØíÍMÑÉФ¹Ñ¡¹ÍÉ  ÀÌØí͹¤(ì(ìIÅեɵ¹Ð¡Ì¤Õ½Ñ%Ð   Ñ̸ȸĸà½È¡¥¡È(ì(ìIÑÕɸY±Õ¡Ì¤=¸MÕÍÌèÀÍÉÉälÁt½¹Ñ¥¹ÌÑ¡¥ÉÍнչÍÑÉ¥¹(ì=¸¥±ÕÉèÉɽÈôÄè9¼¥¹ÑݸÍÑÉ¥¹Ý̽չ(ì(ìÕÑ¡½È¡Ì¤èMµ=­}8(ìQ¡¹­ÌѼY±¥¬½È¡±Á¥¹Ý¥Ñ Ñ¡¹ÜMÑÉ¥¹IáÀ ý̤ ý¤¤¥ÍÍÍÕ(ìôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô()Õ¹}MÑÉ    Ñݸ ÀÌØíÍMÑÉ¥¹°ÀÌØíÍMÑÉаÀÌØí͹°ÀÌØíÙ
Íô´Ä°ÀÌØí¥MIô´Ä¤(%%ÀÌØí¥MIô´Ä=ÈÀÌØí¥MIôÕ±ÐQ¡¸($%%ÀÌØíÙ
Íô´Ä=ÈÀÌØíÙ
ÍôÕ±ÐQ¡¸ÀÌØíÙ
ÍôÀ($%%ÀÌØíÙ
ͱÐìÐì´Ä¹ÀÌØíÙ
ͱÐìÐìÕ±ÐQ¡¸ÀÌØíÙ
ÍôÄ($%1½°ÀÌØíÍ!½±ôÌäìÌäì°ÀÌØíÍM¹MMÑÉÐôÌäìÌäì°ÀÌØíÍM¹M¹ôÌäìÌäì($%]¡¥±MÑÉ¥¹1¸ ÀÌØíÍMÑÉ¥¹¤ÐìÀ($$$ÀÌØíÍM¹MMÑÉÐôMÑÉ¥¹%¹MÑÈ ÀÌØíÍMÑÉ¥¹°ÀÌØíÍMÑÉаÀÌØíÙ
ͤ($$%%9½ÐÀÌØíÍM¹MMÑÉÐQ¡¸á¥Ñ1½½À($$$ÀÌØíÍMÑÉ¥¹ôMÑÉ¥¹QÉ¥µ1Ð ÀÌØíÍMÑÉ¥¹° ÀÌØíÍM¹MMÑÉЬMÑÉ¥¹1¸ ÀÌØíÍMÑÉФ¤´Ä¤($$$ÀÌØíÍM¹M¹ôMÑÉ¥¹%¹MÑÈ ÀÌØíÍMÑÉ¥¹°ÀÌØí͹°ÀÌØíÙ
ͤ($$%%9½ÐÀÌØíÍM¹M¹Q¡¸á¥Ñ1½½À($$$ÀÌØíÍ!½±µÀìôMÑÉ¥¹1Ð ÀÌØíÍMÑÉ¥¹°ÀÌØíÍM¹M¹´Ä¤µÀì
¡È Ĥ($$$ÀÌØíÍMÑÉ¥¹ôMÑÉ¥¹QÉ¥µ1Ð ÀÌØíÍMÑÉ¥¹°ÀÌØíÍM¹M¹¤($%]¹($%%9½ÐÀÌØíÍ!½±Q¡¸IÑÕɸMÑÉÉ½È Ä°À°À¤($$ÀÌØíÍ!½±ôMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹QÉ¥µI¥¡Ð ÀÌØíÍ!½±°Ä¤°
¡È Ĥ¤($%1½°ÀÌØíÉÉåmU ½Õ¹ ÀÌØíÍ!½±¤´Åt($%½ÈÀÌØí¥
ôÄQ¼U    ½Õ¹ ÀÌØíÍ!½±¤´Ä($$$ÀÌØíÉÉålÀÌØí¥
´ÅtôÀÌØíÍ!½±lÀÌØí¥

t($%9áÐ($%IÑÕɸÀÌØíÉÉä(%±Í($%%ÀÌØíÙ
ÍôÕ±Ð=ÈÀÌØíÙ
Íô´ÄQ¡¸ÀÌØíÙ
ÍôÌäì ý¤¤Ìäì($%%ÀÌØíÙ
ͱÐìÐìձйÀÌØíÙ
ͱÐìÐì´ÄQ¡¸ÀÌØíÙ
ÍôÌäìÌäì($%1½°ÀÌØíÉÉäôMÑÉ¥¹IáÀ ÀÌØíÍMÑÉ¥¹°Ìäì ý̤ÌäìµÀìÀÌØíÙ
͵ÀìÀÌØíÍMÑÉеÀìÌäì ¸¨ü¤ÌäìµÀìÀÌØí͹°Ì¤($%%%ÍÉÉä ÀÌØíÉÉä¤Q¡¸IÑÕɸÀÌØíÉÉä($%IÑÕɸMÑÉÉ½È Ä°À°À¤(%¹%)¹Õ¹

Edit:

Forgot a Return...

Edit2:

Changed the default from StringRegExp to regular String Manipulation because most users don't know how to properly use StringRegExp.

Edited by SmOke_N

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

Hi,

great. I hope it will be included in standard, because I think the func is very useful.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • Moderators

Hi,

great. I hope it will be included in standard, because I think the func is very useful.

So long,

Mega

Already submitted it :whistle:

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

... While this strBetween() function is much more complex (which can be good), I've run across a few other strbetween functions.

The following code is pulled from one of the projects. I was using the longer, uncommented function because it was the first thing I tried that worked, while the other two I didn't feel like testing since I already had something that worked. They are much simpler than your function, which may be useful for some, and not useful to others.

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
EndFunc

; ALTERNATIVE 1 to the "stringbetween" function
;Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i')
;   If $iCase <> 'i' Then $iCase = ''
;   $a_Array = StringRegExp ($s_String, '(?' & $iCase & _
;           ':' & $s_Start & ')(.*?)(?' & $iCase & _
;           ':' & $s_End & ')', 3)
;   If @extended & IsArray($a_Array) Then Return $a_Array
;   Return SetError(1, 0, 0)
;EndFunc ;==>_SRE_BetweenEX

; ALTERNATIVE 2 to the "stringbetween" function
;Func _StringBetween($s_String, $s_Start, $s_End = 0)
;   $s_Start = StringInStr($s_String, $s_Start)+StringLen($s_Start)
;   return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End)-$s_Start)
;EndFunc
Edited by Slythfox
Link to comment
Share on other sites

  • Moderators

... While this strBetween() function is much more complex (which can be good), I've run across a few other strbetween functions.

The following code is pulled from one of the projects. I was using the longer, uncommented function because it was the first thing I tried that worked, while the other two I didn't feel like testing since I already had something that worked. They are much simpler than your function, which may be useful for some, and not useful to others.

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
EndFunc

; ALTERNATIVE 1 to the "stringbetween" function
;Func _SRE_BetweenEX($s_String, $s_Start, $s_End, $iCase = 'i')
;   If $iCase <> 'i' Then $iCase = ''
;   $a_Array = StringRegExp ($s_String, '(?' & $iCase & _
;           ':' & $s_Start & ')(.*?)(?' & $iCase & _
;           ':' & $s_End & ')', 3)
;   If @extended & IsArray($a_Array) Then Return $a_Array
;   Return SetError(1, 0, 0)
;EndFunc;==>_SRE_BetweenEX

; ALTERNATIVE 2 to the "stringbetween" function
;Func _StringBetween($s_String, $s_Start, $s_End = 0)
;   $s_Start = StringInStr($s_String, $s_Start)+StringLen($s_Start)
;   return StringMid($s_String, $s_Start, StringInStr($s_String, $s_End)-$s_Start)
;EndFunc
_SRE_Between() I wrote.

_StringBetween() on the Bottom w0uter wrote.

And the other one you are using only finds one instance, where as the function I wrote above returns all instances.

A suggestion is to read the comments, and only post something when it pertains to the actual thread. Again, you've shown your in-ability to read and understand what's presented.

Edit:

Also, as a side note, the _SRE_Between function you are using there will not work with beta higher than 3.2.1.7.

Edited by SmOke_N

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

A suggestion is to read the comments, and only post something when it pertains to the actual thread. Again, you've shown your in-ability to read and understand what's presented.

You're quick to jump.

Yes, this post does not directly relate to the topic of this thread, however, it is a response to a comment of a comment of the topic of this thread. I'm sure you can adjust yourself accordingly. I wrote what I wrote in my comment because I thought that there would be people viewing this forum and wanting something simpler and perhaps faster. Perhaps you are reading me incorrectly, and it is that I over analyze? (Over analyzing to the point that I think about it too much that I miss the basic simple facts, or in this case, I think ahead and answer a question before someone asks it.)

However, to be honest, no I did not understand that the code you wrote does loop for all answers. I didn't a good look at your code and implementation example. And yes, I did read the previous comments before posting, both of which have nothing to do with my comment or did not deal with something that should discourage me for posting what I had posted.

This is a community. We help each other, as cheesy as it sounds.

Anyway, on a lighter note, the script looks good. I'm sure I'll have a use for it later. Bookmarked. :whistle:

Link to comment
Share on other sites

  • Moderators

You're quick to jump.

Yes, this post does not directly relate to the topic of this thread, however, it is a response to a comment of a comment of the topic of this thread. I'm sure you can adjust yourself accordingly. I wrote what I wrote in my comment because I thought that there would be people viewing this forum and wanting something simpler and perhaps faster. Perhaps you are reading me incorrectly, and it is that I over analyze? (Over analyzing to the point that I think about it too much that I miss the basic simple facts, or in this case, I think ahead and answer a question before someone asks it.)

However, to be honest, no I did not understand that the code you wrote does loop for all answers. I didn't a good look at your code and implementation example. And yes, I did read the previous comments before posting, both of which have nothing to do with my comment or did not deal with something that should discourage me for posting what I had posted.

This is a community. We help each other, as cheesy as it sounds.

Anyway, on a lighter note, the script looks good. I'm sure I'll have a use for it later. Bookmarked. :whistle:

Not quick to jump at all. But you implied that the "code" was overly complicated. It's quite "simple" in comparison to what you are currently using versus the results it (the UDF) returns.

And by comments I meant the header.

BTW, if that's all you want (1 result), then w0uters function at the bottom is much faster than the one you are currently using.

In addition, you should do a time test on the functions, you'll see that StringRegExp(), if used correctly, is much faster than standard string manipulation functions. So if speed is something your after, you may want to look at how to use that type of option.

I've provided a simple example, but StringRegExp() is easily 4 to 5 times faster than standard string manipulation as you are using and or as I've presented with an "easy" option for users that don't have the understanding, nor the want to, to understand StringRegExp().

It's been submitted as a UDF to Jdeb, and seemingly accepted unless something changes from now to then. So you will be able to use it via standard UDF's Friday I believe in the newest beta release (3.2.1.12 I believe).

Edited by SmOke_N

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

Not quick to jump at all. But you implied that the "code" was overly complicated. It's quite "simple" in comparison to what you are currently using versus the results it (the UDF) returns.

I was inferring "complicated" as in longer code with more functionality/options. When I meant I was lazy, I really meant I was lazy... and speed wasn't and objective for me. I had just wanted something to work at the time, since I wasn't going to be releasing the script to anyone.

@gafrost, I know what you mean. Sorry about that. :whistle:

Edited by Slythfox
Link to comment
Share on other sites

Great job, this should be a Real String Function. or atleast a sticky cuz stringbetween is often needed

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

  • 1 month later...

I just stumbled upon this gem, and it's fantastic. But running into a slight problem, I'm sure it's on my end perhaps someone could offer a bit of assistance here, it would be much appreciated.

This is actually parsing html and taking out this chunk

385182122" class="fieldStat"><strong>Apepi</strong>

this is the result of running this strBetween() function, after I got all the data from the webpage in the array I'm running the strBetween function again to trim it down to just 'Apepi' by using the following

For $r = 0 to UBound($myArray,1) - 1
      $tmp = _StrBetween($myArray[$r], '<strong>', '</strong>)
Next

and with this it returns nothing even if I use a _arraydisplay to show me whats in the array.

any ideas what i'm doing wrong here, I'm fairly new to this scripting language but have made leaps and bounds over the past few weeks working with it. The search option on these boards is invaluable (never get rid of it or limit my searches please, mr admin lol)

Thanks

Link to comment
Share on other sites

An array is returned from _StrBetween

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

  • Moderators

This was submitted and is now in the current beta versions of AutoIt, which 2 bugs were already fixed in it.

When using the Regular Expression type, you must be sure (and know how) to use the proper style of display.

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

Flippin' weird! after saving the code last night (which I didn't change) exited out of Scite, went to bed, woke up this morning. and the damn code works now. Maybe Internet goblins snuck on my pc last night and fixed the code. Well whatever happen thanks RazorM for the assistance, and once again Smoke_N great work on this.

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