AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Validating a variable

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Tue Jan 22, 2008 8:43 pm    Post subject: Validating a variable Reply with quote

So i wrote a couple of quick hotkeys for when i have to remotely connect to users machines at work, which i do a lot of. All you have to do is highlight the IP address or hostname and hit the correct combination of keys. Ive been trying to find a way that will validate the variable from the clipboard to make sure it is an IP address/Hostname but im completly lost on how to go about it.

Here is a the part of my macro that actually does the work of connecting;
Code:

^+d:: ; Ctrl+Shift+D Dameware Connect
send ^c ; Copies Text
ClipWait  ; Wait for the clipboard to contain text.
Run, C:\Program Files\DameWare Development\DameWare NT Utilities\dwrcc.exe -c: -h: -m:%clipboard% -a:1
Return


Please help...
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Tue Jan 22, 2008 9:09 pm    Post subject: Reply with quote

RegExMatch is good for validating text like this. Recognizing an IP address is fairly easy. Something like this:
Code:
If RegExMatch(clipboard, "^(\d{1,3}\.){3}\d{1,3}$")

That means the pattern of 1 to 3 digits and a dot repeated 3 times followed by another 1 to 3 digits. The ^ and $ mean no other characters can come before or after.

But I'm not sure how you would recognize a host name, unless you have a list of them to compare it to.

By the way, ClipWait won't do any good unless you clear the clipboard before sending ^c. To do that use ClipBoard =
Back to top
View user's profile Send private message
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Tue Jan 22, 2008 9:18 pm    Post subject: Reply with quote

ok so how would you have it make sure the octets are a number from 1 to 255, and all the hostnames are in the same format: ABCDEFG12345678

p.s. you right i forgot the "clipboard =" before the "^c", thanks for pointing that out.
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Wed Jan 23, 2008 12:49 am    Post subject: Reply with quote

manicresin wrote:
ok so how would you have it make sure the octets are a number from 1 to 255,

It's possible tot do that in RegEx, of course but I personally would switch to Loop Parse for that.

manicresin wrote:
and all the hostnames are in the same format: ABCDEFG12345678

Sorry, not quite sure what you mean there, just that in contains only those characters? That combinations of letters and numbers or...
Back to top
View user's profile Send private message
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Wed Jan 23, 2008 12:57 am    Post subject: Reply with quote

Im not too worried about making sure the IP's are a number between 1-255 so ill skip that for now, but the hostename i do need and all the hostnames are made up of 7 letters and 8 numbers. Ive been trying to get the RegEx to work but im not to familer with expressions so all my attemps have failed.
Back to top
View user's profile Send private message
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Wed Jan 23, 2008 1:27 am    Post subject: Reply with quote

Ok so i tried this and it seemed to semi work the way i wanted it to.

Code:

RegExMatch(clipboard, "^(\D{7,7})\d{8,8}$")


but thanks for the help it has tought me some about expresions and i think im starting to get it a little. Smile
Back to top
View user's profile Send private message
skwire



Joined: 18 Jan 2006
Posts: 132
Location: Conway, Arkansas

PostPosted: Wed Jan 23, 2008 10:49 am    Post subject: Reply with quote

Here's what I use within my own apps to validate for an IP:

Code:
RegExMatch( _Search_Term, "^\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$")
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Murp|e



Joined: 12 Jan 2007
Posts: 240
Location: Norway

PostPosted: Wed Jan 23, 2008 1:23 pm    Post subject: Reply with quote

You could use "net view" to get a list of computer hosts currently on the network and you could CMDRet.dll (Search the forums) to avoid having to write the list to a temporary file. That way you could also validate the host names.
Back to top
View user's profile Send private message Visit poster's website
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Wed Jan 23, 2008 8:06 pm    Post subject: Reply with quote

Tskiwire for that RegExMatch it will work perfectly.
As for using net view it wouldnt work in my situation, I work for a global company but from manauser's first example i figured out a way to validate the host names based of contry code and asset number without having to write a list.
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Wed Jan 23, 2008 8:12 pm    Post subject: Reply with quote

manicresin wrote:
Ok so i tried this and it seemed to semi work the way i wanted it to.

Code:

RegExMatch(clipboard, "^(\D{7,7})\d{8,8}$")


but thanks for the help it has tought me some about expresions and i think im starting to get it a little. Smile

This is close, but not quite right. \D will also match non-alphanumaric characters like @#$%^&* so use this instead:

Code:

RegExMatch(clipboard, "^([a-zA-Z]{7})\d{8}$")

I also also changed {7,7} to {7} (no difference, it just looks neater).
Back to top
View user's profile Send private message
manicresin



Joined: 15 May 2007
Posts: 24

PostPosted: Wed Jan 23, 2008 8:47 pm    Post subject: Reply with quote

Cool ill try that, I thought I tried somthing very similar and it didnt work but ill go off that and see what i get. Thanks again for all the help by the way!

[Edit] Here is what i came up with

Code:

IRegExMatch(clipboard, "^(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b$")

RegExMatch(clipboard, "i)^[a-z]{7}\d{8}$")
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group