| View previous topic :: View next topic |
| Author |
Message |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Tue Jan 22, 2008 8:43 pm Post subject: Validating a variable |
|
|
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 |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 901
|
Posted: Tue Jan 22, 2008 9:09 pm Post subject: |
|
|
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 |
|
 |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Tue Jan 22, 2008 9:18 pm Post subject: |
|
|
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 |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 901
|
Posted: Wed Jan 23, 2008 12:49 am Post subject: |
|
|
| 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 |
|
 |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Wed Jan 23, 2008 12:57 am Post subject: |
|
|
| 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 |
|
 |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Wed Jan 23, 2008 1:27 am Post subject: |
|
|
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.  |
|
| Back to top |
|
 |
skwire
Joined: 18 Jan 2006 Posts: 132 Location: Conway, Arkansas
|
Posted: Wed Jan 23, 2008 10:49 am Post subject: |
|
|
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 |
|
 |
Murp|e
Joined: 12 Jan 2007 Posts: 240 Location: Norway
|
Posted: Wed Jan 23, 2008 1:23 pm Post subject: |
|
|
| 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 |
|
 |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Wed Jan 23, 2008 8:06 pm Post subject: |
|
|
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 |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 901
|
Posted: Wed Jan 23, 2008 8:12 pm Post subject: |
|
|
| 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.  |
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 |
|
 |
manicresin
Joined: 15 May 2007 Posts: 24
|
Posted: Wed Jan 23, 2008 8:47 pm Post subject: |
|
|
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 |
|
 |
|