 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
ai4fu
Joined: 09 Dec 2007 Posts: 10
|
Posted: Thu May 29, 2008 9:34 pm Post subject: Trying to read part of a parsed file to clipboard.... |
|
|
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 |
|
| Back to top |
|
 |
ai4fu
Joined: 09 Dec 2007 Posts: 10
|
Posted: Fri May 30, 2008 1:40 am Post subject: Solved my own problem... |
|
|
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.
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 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 |
|
| Back to top |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 1145 Location: The Interwebs
|
Posted: Fri May 30, 2008 2:20 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
ai4fu
Joined: 09 Dec 2007 Posts: 10
|
Posted: Sun Jun 01, 2008 10:35 am Post subject: Thanks for the pointer... |
|
|
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 when I first started.
But those 2 examples will help in future endeavors so muchas gracias...
Jim |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|