Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 06-01-2009, 06:00 PM   #11 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: C# regex help



EDIT: lol just realized line numbers was off for some reason. Image updated.
__________________

Need website help? PM me!

Last edited by CrazeD; 06-01-2009 at 06:10 PM.
CrazeD is offline  
Old 06-01-2009, 09:34 PM   #12 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: C# regex help

better to look for 0.0.0.0 to 255.255.255.255

CodeKeep Snippet : Validate IP Address (C#)

Quote:
Title: Validate IP Address
Language: C#
Description: It has 2 parts. 1 a function that returns true if it is an IP and 2 the Regular Expression it uses to validate it.
Views: 2354
Author: paul b
Date Added: 4/27/2006

office politics is offline  
Old 06-01-2009, 09:44 PM   #13 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: C# regex help

Thanks, that works.

But, I can still add letters after the numbers in the last octet.

Code:
Regex pattern = new Regex(
                @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25"
                + @"[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?"
                + @"<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)",
                RegexOptions.IgnoreCase
                | RegexOptions.CultureInvariant
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
            );
How can I fix that?
__________________

Need website help? PM me!
CrazeD is offline  
Old 06-01-2009, 10:25 PM   #14 (permalink)
S0ULphIRE's Avatar
 
01001100011011110110110

Join Date: Mar 2007

Location: Perth, Australia

Posts: 1,940

S0ULphIRE has a spectacular aura aboutS0ULphIRE has a spectacular aura about

Send a message via MSN to S0ULphIRE
Default Re: C# regex help

Quote:
Originally Posted by CrazeD View Post
Your code didn't work. Firstly I was required to use three numbers in each octet. I want it to accept only 1-3 numbers. I fixed that by changing {2} to {1,3}. Then I could only put two or three numbers in, one number didn't match because you put [0-9] twice. I made it so it was was like [0-9]{1,3}, but that gets me back to the same code I already messed with earlier and I don't get the results I want.

Ok,
Regex pattern = new Regex("[0-9][0-9]{0,2}.[0-9][0-9]{0,2}.[0-9][0-9]{0,2}.[0-9][0-9]{9,2}");

That should work. The fact that there are two [0-9]'s won't matter because the second is set to repeat from 0 to 2 times. If it repeats 0 times then only the first [0-9] will come into play.
__________________
"As a result of all this hardship, dirt, thirst, and wombats, you would expect Australians to be a dour lot. Instead, they are genial, jolly, cheerful, and always willing to share a kind word with a stranger, unless they are an American."
-- Douglas Adams

Click this if I helped you
>>>><<<<
S0ULphIRE is offline  
Old 06-02-2009, 03:53 AM   #15 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: C# regex help

Quote:
Originally Posted by CrazeD View Post
Thanks, that works.

But, I can still add letters after the numbers in the last octet.

Code:
Regex pattern = new Regex(
                @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25"
                + @"[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?"
                + @"<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)",
                RegexOptions.IgnoreCase
                | RegexOptions.CultureInvariant
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
            );
How can I fix that?
Add a word boundry \b to the end.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline  
Old 06-02-2009, 05:53 PM   #16 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: C# regex help

Quote:
Originally Posted by kmote View Post
Add a word boundry \b to the end.
Thanks, that worked for letters, but the following characters are still allowed: ? / . , + = - ! ' " ; : ( ) [ ] { } \ | ` ~

All of those characters, if used after a number, matches. I know that in regex, it makes more logical sense in these situations to only match what you WANT it to look like, and not match everything else. So, how do I do that in this case?

My code now:
Code:
Regex pattern = new Regex(
                @"(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25"
                + @"[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?"
                + @"<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)\b",
                RegexOptions.IgnoreCase
                | RegexOptions.CultureInvariant
                | RegexOptions.IgnorePatternWhitespace
                | RegexOptions.Compiled
            );

__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On