Sign in to follow this  
Followers 0
needhelpplease12

StringRegExpReplace

10 posts in this topic

#1 ·  Posted (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 by needhelpplease12

Share this post


Link to post
Share on other sites



$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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks.

Do you know why my approach didn't work? Do I have to escape the dot or something?

Edited by needhelpplease12

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by Malkey

Share this post


Link to post
Share on other sites

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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Thanks guys

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

My 2 cents :rolleyes:

Using the lazy '?' to get the last link in the string

$new = StringRegExpReplace($string, '(?s).*(http.+?$)', '$1')

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