AutoHotkey Community

It is currently May 26th, 2012, 7:34 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: May 29th, 2008, 10:34 pm 
Offline

Joined: December 9th, 2007, 4:24 pm
Posts: 20
All help gratefully accepted.

I am a RegEx idiot. I barely understand the concept much less how to implement it. I am trying to modify a script I found on here a while back into something more useful. I cannot locate the original (Of course!) but the code is:

Code:
UrlDownloadToFile, http://checkip.dyndns.org/, C:\Documents and Settings\Jim\My Documents\ip.txt
FileRead, ip, C:\Documents and Settings\Jim\My Documents\ip.txt\ip.txt
FileRead, Clipboard, C:\Documents and Settings\Jim\My Documents\ip.txt
Clipboard = %Clipboard%
MsgBox Current IP Address is %Clipboard%


What I want to do is parse "ip.txt" before it gets read to the clipboard and only select the actual numerical IP address (eg 70.9.137.157) and read only that into the clipboard.

Now, let me stop you. I am looking to understand RegEx, not hear about "Oh, why don't you just display your IP address this way". Because I want to understand how RegEx would find that particular part of the text file, including the "." in between the numbers. I cannot find any kind of help for RegEx that says something like "to get all the text between "address:" and "</body", but that is exactly what I am looking for. Some part of RegExMatch that gives me a "start here and grab everything until you get to "X".
:?:

Thanks in advance for helping out a "dabbler", I am not a serious programmer.

Jim


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Solved my own problem...
PostPosted: May 30th, 2008, 2:40 am 
Offline

Joined: December 9th, 2007, 4:24 pm
Posts: 20
After much empirical testing, I managed to produce my own solution. BTW, I failed to make the code generic in the original post, it used my own systems paths in the code. This is a more generic version which should work on systems like mine (WinXP SP2, AHK ver. 1.0.47.05)

Code:
UrlDownloadToFile, http://checkip.dyndns.org/, %A_WorkingDir%\ip.txt ; Downloads index.html from the specified URL and saves it as ip.txt in the computers local working directory (eg. C:\TEMP)
ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
FileRead, Clipboard, %A_WorkingDir%\ip.txt ; Reads ip.txt into the clipboard.
Clipboard := RegExReplace(Clipboard, "i)[a-z\<\>\:\/]*") ; Strips the letters and HTML tag characters and URL characters leaving only the numeric IP address.
Clipboard = %Clipboard% ; Sets the clipboard as a variable.
FileDelete, %A_WorkingDir%\ip.txt ; Deletes ip.txt (all <1kB of it! Gotta clean up after yourself...)
MsgBox Current IP Address is %Clipboard% ; Opens a message box that tells you what your current IP address is.
Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).


I figured most of this out on my own and some "borrowing" from multiple sources, but it would have been much nicer for someone else to have chimed in. I am simply too impatient to wait that long.:lol:

I guess the RTFM that I see in some peoples signatures applies, but geez man, it took forever to solve it and even then it was a slow and painful :wink: process.

I updated the final version so that it should work on any WinXP machine (I hope). Someone let me know if it works on Vista, please.

Thanks.

Jim


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2008, 3:20 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
ai4fu wrote:
I cannot find any kind of help for RegEx that says something like "to get all the text between "address:" and "</body", but that is exactly what I am looking for. Some part of RegExMatch that gives me a "start here and grab everything until you get to "X".


Although you have your code, the way to do the section I have quoted would be:

Code:
RegExMatch(Clipboard, "(?<=address:)\d+\.\d+\.\d+\.\d+(?=</body)", clipboard)


or

Code:
RegExReplace(Clipboard, "address:(\d+\.\d+\.\d+\.\d+)</body", "$1")


Note that this assumes the format is: "address:IP#HERE</body" (note the lack of spaces). There are better ways to search, and I just wanted to give an example of how to capture something between two other things.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 1st, 2008, 11:35 am 
Offline

Joined: December 9th, 2007, 4:24 pm
Posts: 20
My thanks, Krogdor

It is always nice to get another way of doing things. Especially when I don't know how it works to begin with. Believe it or not, I actually understand the 2 examples you left for me now, as opposed to when I started when RegEx was mostly gibberish...was walking around in a state of :shock: when I first started.

But those 2 examples will help in future endeavors so muchas gracias...

Jim


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, chenhaiyang, kkkddd1, Leef_me, Tilter_of_Windmills, tomL and 64 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group