Sign in to follow this  
Followers 0
AnarchOi

regex help

16 posts in this topic

$variable = "http://www.autoitscript.com/forum/index.php"

how can i remove everything before the LAST " / " ? Then show the result in a MsgBox

Using my example, the result would be "index.php"

thanks!

Share this post


Link to post
Share on other sites



Hello, just one more question:

Taking the code, how can i also remove everything after the LAST dot

the result would be "index"

Thanks again!

Share this post


Link to post
Share on other sites

You can try learning by example.

$variable = "http://www.autoitscript.com/forum/index.php"




$n = StringInStr($variable, "/", 0, -1)
$result = StringTrimLeft($variable, $n)

$n = StringInStr($result, ".")
$result = StringLeft($result, $n-1)

ConsoleWrite("Without regex: " & $result & @CRLF)





$result = StringRegExp($variable, ".*/(.*)\.", 3)
$result = $result[0]

ConsoleWrite("With regex: " & $result & @CRLF)

Share this post


Link to post
Share on other sites

Last question, i promise ^^

I want to use a similar code in another script, but on a list of URLs and not only just 1

the list looks like this

http://www.server.com/file.php

http://www.anotherserver.com/anotherfile.php

http://www.hello.com/goodbye.html

(each URL is on a new line)

I want to delete everything before the last " / " on each line

the result would be:

file.php

anotherfile.php

goodbye.html

Thanks again for your help, this script is going to save me hours and hourss of work :D

Share this post


Link to post
Share on other sites

I think Manadar has given you plenty of help as it is.

Perhaps you should read the helpfile.

Search for things like Array, Loop, and maybe FileRead

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I have already read the regex help page, i'm really a newbie especially with regex...

I have tryed many different things but i can't come to something that works...

I even tryed to search the forums and google to see if i could find a forum post answering my question...

Edited by AnarchOi

Share this post


Link to post
Share on other sites

Manadar has provided you with a working example of regex that will do what you want.

You are asking how to do it to multiple items.

That answer is all over the helpfile and forums.

I'll even give you a big hint. Try _FileReadToArray() in the index of the helpfile

Share this post


Link to post
Share on other sites

I just read the help page on FileReadToArray, but now i am even more confused... :D

i don't want to read a file then sort the content to an array (this is what this function will do if i understood correctly??), the string that needs replacement is already stored in $variable

$variable = "http://www.server.com/file.php

http://www.anotherserver.com/anotherfile.php

http://www.hello.com/goodbye.html"

i want to change it to

$variable = "file.php

anotherfile.php

goodbye.html"

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

.... the string that needs replacement is already stored in $variable

$variable = "http://www.server.com/file.php

http://www.anotherserver.com/anotherfile.php

http://www.hello.com/goodbye.html"

i want to change it to

$variable = "file.php

anotherfile.php

goodbye.html"

Try this.

;
Local $variable = "http://www.server.com/file.php" & @CRLF & _
        "http://www.anotherserver.com/anotherfile.php" & @CRLF & _
        "http://www.hello.com/goodbye.html" & @CRLF
Local $sUrl, $sOldVariable = $variable, $sRes = ""

For $i = 1 To 3
    $sUrl = StringRegExpReplace($variable, "^(.*\v+?|$){" & $i & "}(?s).*", "\1") ; Return line no. 1-based
    $sRes &= StringRegExpReplace($sUrl, '^(?s)(?i)(http|ftp|https|file)://(.*?/|.*$)(.*/){0,}(.*), "\4")
Next

$variable = $sRes
MsgBox(0, 'New Contents of "$variable"', "$variable = " & $sOldVariable & @CRLF & "changed to" & @CRLF & @CRLF & "$variable = " & $variable)
;

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Yeah right, this is stupid.

Fuck this, i'm just going to stop posting... I didn't want to get insulted or bother anyone...

It might sound like i'm just begging for codes but i am asking precise questions because i know what i need. And all parts of code that was given to me on this forum were very helpful and i have used it in multiple codes. It's not just copy and paste, after using it one time i understand how it works and i can modify it to suit my needs.

I asked a couple of questions since 2 days on this forum, and so far my script have 300 lines of codes.... (i can post if you want, you will see by yourself it's not only copy and paste)... Oh yeah, i guess i just "kept asking until someone answers" to make all this script...

I thought this was a HELP forum. Like i said i have tryed searching by myself before posting. I have read the manual, searched google, browsed the forums.... It's not my fault if i'm a newbie. I was at this level when i coded PHP and now i can make my own script.

The problem when you start with a new coding language is just to find the precise functions you are looking for and how to properly use them. I thought this forum was for newbies too.

Thanks anyway, but seeing how i'm getting treated i will find a solution myself

Edited by AnarchOi

Share this post


Link to post
Share on other sites

Wow. There's no need to get pissed about this. We did help you and answered all your questions to the best of our abilities, and everything.

I'll try and explain this from our point of view, because you clearly don't understand it.

It might sound like i'm just begging for codes but i am asking precise questions because i know what i need. And all parts of code that was given to me on this forum were very helpful and i have used it in multiple codes. It's not just copy and paste, after using it one time i understand how it works and i can modify it to suit my needs.

If this was true, then you would only need one or two RegEx examples.

I have showed you to modify this URL (http://www.aweb.com/index.php) into:

index.php (Pattern: .*/(.*) )

and into:

index (Pattern: .*/(.*)\. )

See how similar those patterns are? One just has a dot on the end, which means we tell it to stop before the dot. That seems pretty trivial to me. Now I understand you may not get this the first time (took me months of reading and trying to learn regular expressions). That's why I also provided the code to do it with regular string operations. I've used normal string operations for years before I finally understood regular expressions.

Then you come barging in, demanding to be shown regular expressions for your application without showing what you've tried or showing at all that you've put effort into this. Can you imagine how I felt? I needed to learn all this stuff too before I could use it in my projects, so that means that so should you. Or at least make a good effort!

Even your last question was answered by Malkey, and then you make a big post about how we're not treating you delicately enough, that you expected to be treated like a king if you ask a question. This is a free help and support forum, so if multiple members feel that you're doing something wrong; You're doing something wrong.

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