looking for a regex that allows just parts of an ip.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

looking for a regex that allows just parts of an ip.

Post by s0h0 »

Hello,
im looking for a regEx that allows whole IPs or just parts of an IP. I have build a regex that works but with a bug.
It wont allow "192" but "192.", so it expect a dot on the end of the first number sequence... "192.168" is allowed...

Have someone a RegEx that will do that? or can u show me where i have the failure in my regex???

Code: Select all

(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){1,3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?){0,1}

I need the same stuff for a MAC Address. It should allow "-" and ":" seperators..?



I used the search function on this forum and i was looking at google, without success...! Please excuse my bad english...


THANK U!
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

i found a solution for the ip problem :)))

Code: Select all

(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){0,3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?){0,1}


but the problem withe mac is still actually...
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

s0h0 wrote:i found a solution for the ip problem :)))

Code: Select all

(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){0,3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?){0,1}


but the problem withe mac is still actually...
That will still allow "192." and will also allow "000".

You probably meant something like this:

Code: Select all

^([1-9]\d?|1\d\d|2[0-4]\d|25[0-5])(\.([1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){0,3}$
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

OH YES, thats what i was talking about. thats how i imagined it!

Thanks a lot!




now im still looking and testing for a good regex to validate a mac address...
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

s0h0 wrote:...
now im still looking and testing for a good regex to validate a mac address...
Okay, what have you tried? What cases did it fail?
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

hi, thank u for ur reply.

i tried that one.

Code: Select all

^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$
but im a little bit confused... here is a list of macs that valid and macs that not valid:
valid:
0B:6B:08:C4:99:22
0B-6B-08-C4-99-22
aa-aa-aa-aa-aa-aa
0B:6B:08:C4:99:Ab
0B:6B:08:C4:99:A9
invalid:
0B:6B:08:C4:99:AJ
0B:6B:08:C4:99:AS
0B:6B:08:C4:99:G3
OB:6B

i just change the last letter and it says its invalid???
and also just parts of an mac should be allowed...


do u have an idea?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

It's the same idea:

Code: Select all

^(?i)[\da-f]{2}([-:][\da-f]{2}){0,5}$
but note that it also accepts addresses like:

Code: Select all

0B:6B:08:C4-99:Ab
0B:6B-08
IE: both the '-' and the ':' delimiters are used.
If you don't want that to happen, you'll have to go for a slightly more complex solution like this:

Code: Select all

^(?i)[\da-f]{2}(?:([-:])[\da-f]{2}(?:\1[\da-f]{2}){0,4})?$
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

WoW, it works fine! i took the second one.

its realy mystic to me how someone could understand that in detail... it looks more complicated then hyroglyphs...


thank u a lot for ur help prometheuzz u carrier of the gods ;)
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

oh i was a little bit to fast. it worked fine in the regExBuilder Freeware Program. But when i implemented it to my regulerExpressionValidator in ASP.NET then it wont work and allowes all kind of chars,
even this "hahjmkvhjmkh,45454???"

sorry u have an idea why it performs that way?? with the ip expression it worked fine in the regexvalidator...

Code: Select all

      
 <asp:TextBox ID="tbMAC" runat="server" Width="200px"></asp:TextBox>
        <br />
        <asp:RegularExpressionValidator ControlToValidate="tbMac" ForeColor="Red" ID="RegularExpressionValidator1" 
        Text="Please enter a valid MAC Address" runat="server"
        ValidationExpression="^(?i)[\da-f]{2}(?:([-:])[\da-f]{2}(?:\1[\da-f]{2}){0,4})?$"></asp:RegularExpressionValidator>
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

s0h0 wrote:oh i was a little bit to fast. it worked fine in the regExBuilder Freeware Program. But when i implemented it to my regulerExpressionValidator in ASP.NET then it wont work and allowes all kind of chars,
even this "hahjmkvhjmkh,45454???"

sorry u have an idea why it performs that way?? with the ip expression it worked fine in the regexvalidator...

Code: Select all

      
 <asp:TextBox ID="tbMAC" runat="server" Width="200px"></asp:TextBox>
        <br />
        <asp:RegularExpressionValidator ControlToValidate="tbMac" ForeColor="Red" ID="RegularExpressionValidator1" 
        Text="Please enter a valid MAC Address" runat="server"
        ValidationExpression="^(?i)[\da-f]{2}(?:([-:])[\da-f]{2}(?:\1[\da-f]{2}){0,4})?$"></asp:RegularExpressionValidator>
Unfortunately, I am not familiar with this ASP.NET regulerExpressionValidator. A couple of things you may try:
- maybe the single backslashes need extra escaping? So instead of "\d", try "\\d" (and "\1" would become "\\1")
- does it support back references?

If it doesn't support back references, then my second suggestion should be something like this:

Code: Select all

^(?i)[\da-f]{2}((-[\da-f]{2}){0,5}|(:[\da-f]{2}){0,5})$
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

ahh, thank u alot. i will try this... the escape thing isnt the problem cause i would see that in the enviroment...





i want make drama but i found a lil bug in the ip regex. the ip 10.0.0.0 is signed as invalid...!? i couldnt worked it ou why.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

Code: Select all

^(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){0,3}$
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

ahh, thank u. u are my personal hero for today :)))



but that mac stuff is awfull... both tips of u wont work... it always allows everything. thats kind a strange...
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: looking for a regex that allows just parts of an ip.

Post by prometheuzz »

s0h0 wrote:ahh, thank u. u are my personal hero for today :)))



but that mac stuff is awfull... both tips of u wont work... it always allows everything. thats kind a strange...
I'm pretty sure it's some ASP thing because Perl, PHP and Java all have no problem with that regex.
Your best bet is to ask for clarification in an ASP forum.

Best of luck.
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: looking for a regex that allows just parts of an ip.

Post by s0h0 »

yes ur propably right... i will do that and report the solution if available...


THANK U so far :)
Post Reply