Thread: C# regex help
View Single Post
Old 06-01-2009, 04:50 PM   #9 (permalink)
CrazeD
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,690

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 S0ULphIRE View Post
Regex pattern = new Regex("[0-9][0-9]{2}.[0-9][0-9]{2}.[0-9][0-9]{2}.[0-9][0-9]{2}");

try that.

edit: this might be easier:

Regex pattern = new Regex("[\d][\d]{0,2}.[\d][\d]{0,2}.[\d][\d]{0,2}.[\d][\d]{0,2}");

C# and Regex
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.

Quote:
Originally Posted by kmote View Post
I also hate regex but in theory something like this might work
\d{1,3}(\.\d{1,3}){3}

this should look for 0-9 between 1 and 3 times then a dot followed by 1-3 numbers 3 times. In practise limited repetition ({1,3} etc.) is not always supported and you may be forced to use + instead. Occasionally \d isn't supported either in which case you can replace it with [0-9].

Filled with hate
Your code also didn't work. The \d and \. didn't agree with it, so I made those [0-9] and . respectively. It worked, however, as long as three numbers are present I can add as many letters as I want.

For example, this matches: 1a1a1.2b2b2.3c3c3.4d4d4

I tried to fix it with this: [0-9]{1,3}[^A-Za-z](.[0-9]{1,3}){3}

But that doesn't help at all. Why?
__________________

Need website help? PM me!
CrazeD is offline