needhelpplease12 Posted May 24, 2014 Posted May 24, 2014 (edited) Hi, I want to replace a fixed substring and return the rest of the string. I have the string $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" and want to remove the first part of the string which is http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url= I have a few of these links and the first part always is the same. The part after it changes (the image name) so I want to extract that url to the image. I tried with the following code but it returns the entire original string unchanged: $new = StringRegExpReplace($string, "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=", '') MsgBox(0, "", $new) How to get the last part Please? Edited May 24, 2014 by needhelpplease12
Iczer Posted May 24, 2014 Posted May 24, 2014 $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" $new = StringRegExpReplace($string, ".+url=", '') MsgBox(0, "", $new)
somdcomputerguy Posted May 24, 2014 Posted May 24, 2014 Or this, which will return exactly the same thing. AutoIt can skin a cat in many ways! $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" $newstring = StringSplit($string, "url=", 1) MsgBox(0, "", $newstring[2]) - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change.
needhelpplease12 Posted May 24, 2014 Author Posted May 24, 2014 (edited) Thanks. Do you know why my approach didn't work? Do I have to escape the dot or something? Edited May 24, 2014 by needhelpplease12
somdcomputerguy Posted May 24, 2014 Posted May 24, 2014 Do I have to escape the dot or something?Well escaping the dot in the original string returns the whole string. So it's probably something else.. My RegEx knowledge is very limited, although I have been trying to improve it a bit. But I'm glad cat's have nine lives.. $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" $replaced = StringReplace($string, "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=", '') MsgBox(0, "", $replaced) - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change.
Malkey Posted May 24, 2014 Posted May 24, 2014 (edited) Hi, I want to replace a fixed substring and return the rest of the string. I have the string $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" and want to remove the first part of the string which is http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url= I have a few of these links and the first part always is the same. The part after it changes (the image name) so I want to extract that url to the image. I tried with the following code but it returns the entire original string unchanged: $new = StringRegExpReplace($string, "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=", '') MsgBox(0, "", $new) How to get the last part Please? For correctness the dots should be escaped. But because the dot stands for any one character, including the dot "." character, not escaping the dot character still works. However the "?" character in the regular expression pattern makes the previous character greedy. So if the question mark "?" is not escaped "?" the RE pattern will not match because of the missing "?" character in the RE pattern. Local $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" Local $new = StringRegExpReplace($string, "(http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php\?url=)", "") MsgBox(0, "", $new) Edited May 24, 2014 by Malkey
UEZ Posted May 24, 2014 Posted May 24, 2014 You don't need necessarily regex to cut the right part. It will work also with the string functions. E.g. $string = "http://domain.com/asd/cbcc507d86c16fec3947370ac5849ac0/http://www.domainxyz.org/out.php?url=http://www.otherdomain.org/a/s/d/123.jpg" ConsoleWrite(StringTrimLeft($string, StringInStr($string, "url=", 0, -1) + 3) & @CRLF) Br, UEZ 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
somdcomputerguy Posted May 24, 2014 Posted May 24, 2014 (edited) My RegEx knowledge is very limited, although I have been trying to improve it a bit.For correctness the dots should be escaped. But because the dot stands for any one character, including the dot "." character, not escaping the dot character still works.However the "?" character in the regular expression pattern makes the previous character greedy. So if the question mark "?" is not escaped "?" the RE pattern will not match because of the missing "?" character in the RE pattern.My knowledge improves a bit more.. Thanks Malkey. Edited May 24, 2014 by somdcomputerguy - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change.
mikell Posted May 24, 2014 Posted May 24, 2014 My 2 cents Using the lazy '?' to get the last link in the string $new = StringRegExpReplace($string, '(?s).*(http.+?$)', '$1')
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now