Jump to content
shalu

How can I get dynamic position of a substring?

Recommended Posts

shalu

Hi..I have a substring ABC. 8 places after ABC I have another substring XYZ. I want to validate that XYZ is displayed 8 places after ABC, for which I need the position of substring ABC. How can I get that? XYZ repeats in many places, so I cannot blindly go and search for it. I am looking for the substring XYZ exactly 8 places after ABC. Once I get the position of ABC, using StringMid function I can validate XYZ.

Please help.

Share this post


Link to post
Share on other sites
iamtheky

something like

$string = "ABC1234XYZABC12345678XYZABC123456XYZABC123XYZ"
$pattern = "ABC........XYZ"
StringRegExp($string , $pattern , 1)
$offset = @Extended
msgbox(0, '' ,"ABC offset = " & $offset - stringlen($pattern) - 1 & @LF & "XYZ offset = " & $offset - 4)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
shalu

I got a solution and its working fine

$iPosition= StringInStr($sData_Clip,$ABC)
$Status=StringMid($sData_Clip_2,$ABC+8,4)

If XYZ is present, $Status will have it and I can verify that

Share this post


Link to post
Share on other sites
UEZ

Try this:

$s1 = "ABC"
$s2 = "XYZ"

$sText = "Hi..I have a substring ABC. 8 places after ABC I have another substring XYZ. I want to validate that XYZ is displayed 8 places after ABC, for which I need the position of substring ABC. How can I get that? XYZ repeats in many places, so I cannot blindly go and search for it. ABC12345678XYZ. I am looking for the substring XYZ exactly 8 places after ABC. Once I get the position of ABC, ABC87654321XYZ, using StringMid function I can validate XYZ."
ConsoleWrite("v1" & @CRLF)
Check1($sText, $s1, $s2)
ConsoleWrite("---------------------" & @CRLF)
ConsoleWrite("v2" & @CRLF)
Check2($sText, $s1, $s2)

Func Check1($sText, $s1, $s2, $iDist = 8, $iCase = 0)
    Local $aPos[10000], $i = 1, $j = 0, $iPos
    Do
        $iPos = StringInStr($sText, $s1, $iCase, 1, $i)
        If Not $iPos Then ExitLoop
        $aPos[$j] = $iPos
        $j += 1
        $i = $iPos + 1
    Until False
    ReDim $aPos[$j]
    Local $iLen1 = StringLen($s1)
    Local $iLen2 = StringLen($s2)
    For $i = 0 To UBound($aPos) - 1
        Switch $iCase
            Case 0
                If StringMid($sText, $aPos[$i] + $iLen1 + $iDist, $iLen2) = $s2 Then ConsoleWrite("Found at pos " & $aPos[$i] & ": " & StringMid($sText, $aPos[$i], $iLen1 + $iLen2 + $iDist) & @CRLF)
            Case 1
                If StringMid($sText, $aPos[$i] + $iLen1 + $iDist, $iLen2) == $s2 Then ConsoleWrite("Found at pos " & $aPos[$i] & ": " & StringMid($sText, $aPos[$i], $iLen1 + $iLen2 + $iDist) & @CRLF)
        EndSwitch
    Next
EndFunc

Func Check2($sText, $s1, $s2, $iDist = 8, $iCase = 0)
    Local $aFind, $i = 1, $j = 0
    Switch $iCase
        Case 0
            $aFind = StringRegExp($sText, "(?i)" & $s1 & ".{" & $iDist & "}" & $s2, 3)
        Case 1
            $aFind = StringRegExp($sText, $s1 & ".{" & $iDist & "}" & $s2, 3)
    EndSwitch
    ReDim $aFind[UBound($aFind) + 1]
    Local $iLen1 = StringLen($s1)
    Local $iLen2 = StringLen($s2)
    Do
        $iPos = StringInStr($sText, $aFind[$j], $iCase, 1, $i)
        If Not $iPos Then ExitLoop
        ConsoleWrite("Found at pos " & $iPos & ": " & StringMid($sText, $iPos, $iLen1 + $iLen2 + $iDist) & @CRLF)
        $i = $iPos + 1
        $j += 1
    Until False

EndFunc

 

Edited by UEZ
Small corrections
  • Like 1

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
jchd

Why not use the (essentially) one-liner in post #2?


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
AspirinJunkie

an even shorter form to retrieve the offset directly from stringregexp without following calculations:

$sString = "ABC1234XYZABC12345678XYZABC123456XYZABC123XYZ"

StringRegExp($sString, "ABC.{8}(?=XYZ)", 1)
MsgBox(0, '', "XYZ offset = " & @extended)

 

Edited by AspirinJunkie

Share this post


Link to post
Share on other sites
junkew

Reading what OP is asking

"Once I get the position of ABC, using StringMid function I can validate XYZ. "

$sString = "ABC1234XYZABC12345678XYZABC123456XYZABC123XYZ"

StringRegExp($sString, "ABC.{8}(?=XYZ)", 1)
if @extended>0 then
    if mid($sString,@extended,3)="XYZ"then
        MsgBox(0, '', "Yes its a valid XYZ " & @extended)
    else
        MsgBox(0, '', "No, its not a valid ABC " & @extended)
    endif
endif

Probably the problem the OP has has to be elaborated on a little to give a proper piece of working code

@Shalu:  most likely regular expression functions can solve your problem
https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm

https://www.autoitscript.com/autoit3/docs/functions/StringRegExpReplace.htm

 

Share this post


Link to post
Share on other sites
shalu

Thanks for all your reply. Please see #3. I got that working fine :)

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

×