Sign in to follow this  
Followers 0
pingpong24

extracting data of a STRING, then replacing it

14 posts in this topic

:P

hi, i need extract certin data of a string

i.e.

$String = "http://www.autoitscript.com/forum/index.php?act=post&do=new_post&f=2"

i want to extract:

A: "/forum/inde.php?"

B: "act= verable" just the verable

then replace the verable, so i want basically change act="POST" to act=$verable

I tried using StringReplace and but didnt work.

in otherwords how do i search for certin phase in a string and say:

$string = "123abcdefghijklk456789"

find inside $string, then from "3" to "4" store as $X

and i want to do this also

$string = "123abcdefgh4567"

replace what is between "3" and "4" with $x

thank you

sorry i couldnt make it any clear

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you almost have it;

$First=find 1st string in variable to get position

$second=then find second

$count=$second-$first-1

then $middle=stringmid $var,$first, $count

$firstbit=stringmid $var,1, $first-1

$lastbit=stringmid $var,$second+1

$string= $firstbit & $middle & $lastbit

does that help??

Edited by Rick

Who needs puzzles when we have AutoIt!!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

in otherwords how do i search for certin phase in a string and say:

$string = "123abcdefghijklk456789"

find inside $string, then from "3" to "4" store as $X

and i want to do this also

$string = "123abcdefgh4567"

replace what is between "3" and "4" with $x

thank you

sorry i couldnt make it any clear

to find or replace a string in a string wich must match a special Pattern try StringRegExpReplace

$string = "123abcdefghijklk456789" 
$newString = StringRegExpReplace($string,"[a-zA-Z]+","yourString")
If @error = 0 Then
    msgbox(64,"Result",$newString)
ElseIf @error = 1 Then
    msgBox(64,"Error","Count invalid")
ElseIF @error = 2 Then
    msgBox(64,"Error","Pattern invalid")
EndIf

... you need a betaversion for this example ....

Edited by jonk

Share this post


Link to post
Share on other sites

sorry, no of this helped.

sorry i think i didnt make clear:

$string = "gjsagjkajkgjalsgjls624623ajgkjlgkagajgas359283"

from that string say i wanted to extract some data

$name = somefunction($string, jka -to- 62)

this imaginry funcation would extract from the string "jka" 'jkgjalsgjls' "62"

jka = starting point

62 = end

thats all i want to do, not extract jka, or 62 but use that as a starting point and end.

i can do it in php very easily. how can i do it in AUTOIT

Share this post


Link to post
Share on other sites

$string = "gjsagjkajkgjalsgjls624623ajgkjlgkagajgas359283"
;~ "jka" 'jkgjalsgjls' "62"
$a_Result = StringRegExp($string,"(jka)([^62]*)",1)
If @error = 0 Then
    msgbox(64,"Result",$a_Result[1])
ElseIf @error = 1 Then
    msgBox(64,"Error","Count invalid")
ElseIF @error = 2 Then
    msgBox(64,"Error","Pattern invalid")
EndIf

well, some slight changes...and tadaaa? :P

Share this post


Link to post
Share on other sites

arrrrrgh i am trying it to implement it on my script i cant figure out how to use it, this funcation is so retarded and documentation sucks who ever wrote it should be shoot.

arrrgh :P 2 hours spent and cant get it to work.

why not just make it simple like this : StringRegExp($string, (begaing)(end))

had to be extra and add other stuff.

oh well ill just pm the string to jonk and get him to do the pattern.

Share this post


Link to post
Share on other sites

arrrrrgh i am trying it to implement it on my script i cant figure out how to use it, this funcation is so retarded and documentation sucks who ever wrote it should be shoot.

arrrgh :P 2 hours spent and cant get it to work.

why not just make it simple like this : StringRegExp($string, (begaing)(end))

had to be extra and add other stuff.

oh well ill just pm the string to jonk and get him to do the pattern.

jep, regular expressions sometimes make me mad too :lmao: . I am using a nice

tool to test my patterns:

RegExp-Coach

Share this post


Link to post
Share on other sites

arrrrrgh i am trying it to implement it on my script i cant figure out how to use it, this funcation is so retarded and documentation sucks who ever wrote it should be shoot.

arrrgh :P 2 hours spent and cant get it to work.

why not just make it simple like this : StringRegExp($string, (begaing)(end))

had to be extra and add other stuff.

oh well ill just pm the string to jonk and get him to do the pattern.

Well, I find the documentation really good personally, I think if I'm ever unclear about something, it's not a poor documentation example, it's poor misinterpretation on my behalf.

Dim $String = 'gjsagjkajkgjalsgjls624623ajgkjlgkagajgas359283'
Dim $Starting = 'jka'
Dim $Ending = '62'

MsgBox(0, "Test", _GetString($String, $Starting, $Ending))

Func _GetString($String, $Starting, $Ending)
    Local $OutPut = ''
    $StL = StringTrimLeft($String, StringInStr($String, $Starting) - 1)
    $SeL = StringLeft($StL, StringInStr($StL, $Ending) + 1)
    Return $SeL
EndFunc

[center]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.[/center]

Share this post


Link to post
Share on other sites

Here's the first Method I did, and the second one is using jonk's idea using StringRegExpReplace()... This should get you going at least, making them into little UDF's.

Dim $String = 'gjsagjkajkgjalsgjls624623ajgkjlgkagajgas359283'
Dim $Starting = 'jka'
Dim $Ending = '62'

MsgBox(0, "Test", _GetStringMethod1($String, $Starting, $Ending))

Func _GetStringMethod1($String, $Starting, $Ending)
    $StL = StringTrimLeft($String, StringInStr($String, $Starting) - 1)
    $SeL = StringLeft($StL, StringInStr($StL, $Ending) + 1)
    Return $SeL
EndFunc

MsgBox(0, "Test2", _GetStringMethod2($String, $Starting, $Ending))

Func _GetStringMethod2($String, $Starting, $Ending)
$a_Result = StringRegExp($String,'('&$Starting&')([^'&$Ending&']*)',1)
 If @error = 0 And @extended Then Return $Starting & $a_Result[1] & $Ending
EndFunc

[center]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.[/center]

Share this post


Link to post
Share on other sites

thank you guys fro trying to help though non of your script helped me at all. smokn you came close but your script returns the ending.. so that wasnt any help and it was hard to understand your script :P

Func _RequestString($String, $Starting, $Ending)
$startingsize = StringLen($Starting)
$endsize = StringLen($Ending) + $startingsize + 3
$location1 = StringInStr($String, $Starting, 1) + $startingsize 
MsgBox(0,"",$location1)
$location2 = StringInStr($String, $Ending, 1) - $endsize 
MsgBox(0,"",$endsize)

$result = StringMid($String, $location1, $location2)
Return $result

EndFunc

there you i hope they add this to the autoitUDF

Share this post


Link to post
Share on other sites

thank you guys fro trying to help though non of your script helped me at all. so that wasnt any help

what about the script I pm you?

$string = "http://url/announce.php?passkey=ba5e00c1a3272c7cf970f40acdffac4c?info_hash=%7DM%D0%98%0Ef%0B%E2%A2%16I%00%C5%05%8B%F4%BF%E9J%3F&peer_id=%2DBC0060%2D2%2D0%87z%1D%BF%88p2%8F%1E&port=6999&natmapped=1&localip=127.0.0.1&uploaded=0&downloaded=0&left=0&numwant=200&compact=1&no_peer_id=1&key=18478&event=started%22%22"
;~ "jka" 'jkgjalsgjls' "62"
$a_Result = StringRegExp($string,"=([^&]*)",3)
If @error = 0 Then
    if isArray($a_Result) then
        for $i=0 to ubound($a_Result)-1 Step 1
            msgbox(64,"Result",$a_Result[$i])
        next
    EndIf
ElseIf @error = 1 Then
    msgBox(64,"Error","Count invalid")
ElseIF @error = 2 Then
    msgBox(64,"Error","Pattern invalid")
EndIf

it worked for me. :P

Share this post


Link to post
Share on other sites

thank you guys fro trying to help though non of your script helped me at all. smokn you came close but your script returns the ending.. so that wasnt any help and it was hard to understand your script :P

Func _RequestString($String, $Starting, $Ending)
$startingsize = StringLen($Starting)
$endsize = StringLen($Ending) + $startingsize + 3
$location1 = StringInStr($String, $Starting, 1) + $startingsize 
MsgBox(0,"",$location1)
$location2 = StringInStr($String, $Ending, 1) - $endsize 
MsgBox(0,"",$endsize)

$result = StringMid($String, $location1, $location2)
Return $result

EndFunc

there you i hope they add this to the autoitUDF

If my script was "hard" to understand, then I can see why your having trouble with the help file. Maybe you should explain in more detail next time exactly what you want to do.

If you didn't want the 'Starting' or 'Ending' strings to be in the string pulled, then you should have stated that, btw... your's still leaves the first delimeter '6' or the '62' in the string, was that a desired affect, because I didn't see that either.

Dim $String = 'gjsagjkajkgjalsgjls624623ajgkjlgkagajgas359283'
Dim $Starting = 'jka'
Dim $Ending = '62'

; just the string between the $Starting and $Ending
MsgBox(0, "Test1", _GetStringMethod($String, $Starting, $Ending))
Func _GetStringMethod($String, $Starting, $Ending)
$a_Result = StringRegExp($String,'('&$Starting&')([^'&$Ending&']*)',1)
If @error = 0 And @extended Then Return $a_Result[1] 
EndFunc

; with your 1st delimeter 'Ending' string still there
MsgBox(0, "Test1", _GetStringMethod2($String, $Starting, $Ending))
Func _GetStringMethod2($String, $Starting, $Ending)
$a_Result = StringRegExp($String,'('&$Starting&')([^'&$Ending&']*)',1)
If @error = 0 And @extended Then Return $a_Result[1] & StringLeft($Ending, 1)
EndFunc

Next time giving an example (which one of your post explained a 'bit') give the 'Exact Desired' output you want to receive:

this imaginry funcation would extract from the string "jka" 'jkgjalsgjls' "62"

your example here, I interpreted to mean that you wanted all 3 of those strings in one.

[center]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.[/center]

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