Jump to content
JohnOne

Regexp Warlocks

Recommended Posts

JohnOne

I'm shamelessly just asking this without even trying, because I'm just pathetic at doing regex. Plus I know some of the magicians here enjoy the challenge. Plus I could not find what I need after a web search.

What I'm after is a regexp pattern to validate a LAN IP address, which I believe can be...

10.0.0.0        -   10.255.255.255
172.16.0.0      -   172.31.255.255
192.168.0.0     -   192.168.255.255

My thoughts are that there would probably have to be three at least, or possibly more pattern passes to validate that an IP is within one of the above bounds.

So if it interests you, great, and if it annoys you then feel free to give me a piece of your mind, and thanks for looking in all the same.

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
BrewManNH

RegEx is terrible for valid IP addresses. Try this one, totally non-regex, modify as you need to. It just looks at the IP address you send to it and check to make sure it's valid. You can do further checks on the returned information to verify that it's within the ranges you need it to be.

  • Like 1

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jchd

So let's try dancing:

Local $aIPs = [ _
    "10.0.0.0", "10.1.2.3", "10.255.255.255", _
    "172.16.0.0", "172.25.1.2.3", "172.31.255.255", _
    "192.168.0.0", "192.168.1.2", "192.168.255.255", _
    "12.34.56.78", "123.456.789.0", "192.168.0.555" _
]

For $MyIP In $aIPs
    ConsoleWrite($MyIP & " is a" & (_Valid_LAN_IP($MyIP) ? " " : "n in") & "valid LAN IP address" & @LF)
Next

Func _Valid_LAN_IP($ip)
    Return StringRegExp($ip, _
                                "(?x) (?(DEFINE) (?<byte> 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d) )" & _
                                " ^ (" & _
                                "      10 (\.(?&byte)){3} | " & _
                                "      172 \. ( 1 [6-9] | 2\d | 3[01] ) (\.(?&byte)){2} | " & _
                                "      192 \. 168 (\.(?&byte)){2} | " & _
                                ") $")
EndFunc

 

  • Like 2

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jchd

I'm not so sure: RegExps have a very precise semantics (despite a rather cryptic syntax), contrary to human spoken/writen languages which all have a fuzzy multi-level semantics and a fairly complex and often distorded syntax. Basic example: "The horse is out of the barn". This can refer to a bacteria, virus, bug or invasive plant, some news (true fact or made up rumor), a computer worm, a groundshaking secret that should never have been revealed, the launch of some marketting campaign, mission Apollo 38, an actual horse, ...

It would be pretty simple to convert PCRE syntax into a series of function calls and simple structures, both comparable to, say, AutoIt.

BTW, the (?x) option at the start of the regexp allows subsequent unsignificant whitespaces, only to ease reading the thing.

Also the use of an internal subroutine (named byte) is handy in this use case to avoid duplication of long patterns.

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
JohnOne

I just go blank when I look at patterns, with a real dumbstruck and stupefied look on my face, much like when someone hands me one those fancy smart phones.

It's not through laziness, or being stupid, It's real and I cannot control it. Proper weird like a shrink would have a field day with me about it.

And they make me sleepy.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jchd

I've recorded a slightly different version as https://regex101.com/r/rU1iI6/1 This one scans a series of lines with IPs and select the valid LAN ones (use option 3 then to obtain the resulting array).

Paste the regexp in the "tester" page and read the "explanation" text. You'll certainly agree that this text reads like pseudo-code (it is, in fact) which one can obviously translate into an ad hoc set of function calls and simple block constructs.

Granted creating correct complex regexps requires a little bit of "regexp thinking" due to the particular way matching attempts work. It always helps translate the regexp goal into plain english in the "explanation" box style, then convert that into PCRE syntax. Some advanced features require good experience but this isn't breaking news. Very few AutoIt noobs don't start coding for a complex 5-GUI app using custom skin and requiring hooking dozens of cryptic Windows messages.

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×