Sign in to follow this  
Followers 0
dexto

string problem

15 posts in this topic

$txt = "<a> useless text <b> 5 users </a"
ConsoleWrite(StringInStr($txt, '>', 1, -1, 5)&@CRLF)

Output: 3

How is that possible?

P.S. Using AutoIt v3.3.6.0

Share this post


Link to post
Share on other sites



$txt = "<a> useless text <b> 5 users </a"

ConsoleWrite(StringInStr($txt, '>', 1, -1, 5)&@CRLF)

This is correct: return the 1-based position of the first occurence of '>' searching from right to left begining at position 5 using a case-sensitive compare.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

$txt = "<a> useless text <b> 5 users </a"

ConsoleWrite(StringInStr($txt, '>', 1, -1, 5)&@CRLF)

This is correct: return the 1-based position of the first occurence of '>' searching from right to left begining at position 5 using a case-sensitive compare.

Starting from the right to left first occurrence of the string '>' is not 3 but 20.. so, function should return 20?

Like so: StringInStr($txt, '>', 1, -1)

Output: 20

Share this post


Link to post
Share on other sites

You are telling it to start searching from position 5 which would be the "u" in useless. The first occurrence from right to left from there is the third position in the string.

Share this post


Link to post
Share on other sites

You omit starting at position 5. So Position 5 is your rightmost position.

Equivalent code:

$cmp = StringLeft($txt, 5)

$cmp = _StringReverse($cmp)

$res = StringLen($cmp) - StringInStr($cmp, '>') + 1


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

oh.... now i see! Offset of the string is also direction dependent.

Edit: never mind I don't see.

Edited by dexto

Share this post


Link to post
Share on other sites

You are telling it to start searching from position 5 which would be the "u" in useless. The first occurrence from right to left from there is the third position in the string.

$txt = "<a> useless text <b> 5 users   </a"
ConsoleWrite(StringInStr($txt,'>',0,-1,5)&@CRLF)

Its not 3rd in this case... yet output is 3

Share this post


Link to post
Share on other sites

You omit starting at position 5. So Position 5 is your rightmost position.

Equivalent code:

$cmp = StringLeft($txt, 5)

$cmp = _StringReverse($cmp)

$res = StringLen($cmp) - StringInStr($cmp, '>') + 1

5 is not the rightmost position, its "start [optional] The starting position of the search. "

Share this post


Link to post
Share on other sites

In other words "negative occurrence to search from the right side" invalidates the "start" and "count" of the function and "finds first occurrence" instead.

So:

StringInStr($txt,'>',0,-1,5)
; Same as
StringInStr($txt,'>',0,1)
; or
StringInStr($txt,'>')

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

In fact....

$txt = "<a> useless text <b> 5 users   </a"
ConsoleWrite(StringInStr($txt,'>',0,-1,6,8)&@CRLF)
ConsoleWrite(StringInStr($txt,'>',0,-1,5)&@CRLF)
ConsoleWrite(StringInStr($txt,'>',0,-1,5)&@CRLF)
ConsoleWrite(StringInStr($txt,'>',0,1)&@CRLF)
ConsoleWrite(StringInStr($txt,'>')&@CRLF)

Are all the same.

P.S.

Sorry for multiple concurrent posts...

Edited by dexto

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

No, you still don't get it. The code is equivalent to what I gave (except that I forgot the case-isensitive parameter, which is irrelevant for this example).

Here is the match at 3 (down arrow)

12↓

<a> useless text <b> 5 users </a

1234 start comparing here, to the left

Edit: argh, formatting once again destroyed. Another try.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Replying to "all the same": in this particular case, yes, but if one changes a little the input:

$txt = "a>us>eless>text <b> 5 users </a"
ConsoleWrite(StringInStr($txt,'>',0,-1,12,8)&@CRLF)
ConsoleWrite(StringInStr($txt,'>',0,-1,5)&@CRLF)
ConsoleWrite(StringInStr($txt,'>',0,1)&@CRLF)
ConsoleWrite(StringInStr($txt,'>')&@CRLF)

The last two statements are indeed exactly identical (default values)


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

$txt = "Why is this so hard for you to understand?"

; Position 5 is the "i" in is
; This returns 3 because the "y" in why is the first occurrence from right to left of the "i" in is
ConsoleWrite(StringInStr($txt, 'y', 1, -1, 5) & @CRLF)

; Position 10 is the "i" in this
; This returns 0 because StringInStr does not wrap back around and search the remainder of the string
ConsoleWrite(StringInStr($txt, 'd', 1, -1, 10) & @CRLF)

; Position 15 is the space between so and hard
; This returns 14 because the "o" in so is the first occurrence from right to left of the space between so and hard
ConsoleWrite(StringInStr($txt, 'o', 1, -1, 15) & @CRLF)

Edited by big_daddy

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

He... well..

I missed that count was counting down the characters from the start not the parameters to point the scope of the search (like StringMid would).

Thank you guys for the patience! Big props for that!

Edited by dexto

Share this post


Link to post
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
Sign in to follow this  
Followers 0