Jump to content

String Regular Expression problem


Recommended Posts

suppose that we have this string "192.168.1.1a"

i want to search and find if the string has an alpha character or a symbol character to dismiss the strings like this....

i tried StringRegExp("192.168.1.1a","[:alpha:]",0) but it didn't worked!

any of the [:classes:] hasn't worked for me ...

what's the wrong i'm doing?

the finest Blues Jazz Rock by VL and SpecKtotallynoob.com tutorials about SysAdmins NetAdmins SecAdmins
Link to comment
Share on other sites

Func _IsIP($sIP)
    ;funkey 07.12.2009
    Local $Pattern = "^(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]?)$"
    If StringRegExp($sIP, $Pattern) Then Return 1
    Return 0
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Func _IsIP($sIP)
    ;funkey 07.12.2009
    Local $Pattern = "^(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]?)$"
    If StringRegExp($sIP, $Pattern) Then Return 1
    Return 0
EndFunc

funkey THNX a lot for your help!!!!

Could you explain me the logic of your pattern?

I find reg.expression very difficult to understand ...

If you could do this, you'll help much more than your func did!!!

thnx anyway!!

the finest Blues Jazz Rock by VL and SpecKtotallynoob.com tutorials about SysAdmins NetAdmins SecAdmins
Link to comment
Share on other sites

I am not that good at StringRegExp too, but a few things I know.

^ at the beginning and $ at the end means from beginning of the string to the end of the string

25[0-4] means from 250 till 254

| means OR

2[0-4][0-9] means from 200 till 249

[01]? means 0 or 1 could be but is not needed

[0-9][0-9] means from 00 till 99

\. means a . because . stands for any sign

And all this 4 times :mellow:

I hope that I explained it right.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Sorry funkey but that Reg Ex is way too long :mellow: Although it does check everything nicely.

To verify an IP address has the right format, you can just use this (it does NOT check if any of the bytes exceeds 255 though):

$ip1 = "192.168.0.1"
$ip2 = "192.a12.0.1"
$regEx = "^(?:\d{1,3}\.){3}\d{1,3}$"
; Or to simply check whether a string consists ONLY of digits (\d) and points (.):
; $regEx = $regEx = "^[\d.]+$"

ConsoleWrite(StringRegExp($ip1, $regEx) & @CR) ; Prints 1
ConsoleWrite(StringRegExp($ip2, $regEx) & @CR) ; Prints 0

@funkey

You should at least shorten it to this:

$regEx = "^(?:(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]?)$"

; And even slightly shorter -- why did you use [01]? This would also allow stuff like 01 and 05 etc instead of 1 and 5, but that's not standard format.
$regEx = "^(?:(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}" & _
             "(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})$"

I somehow think it should be possible to shorten it some more but ah well, 2 lines is acceptable.

Edited by dani
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...