Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-30-2009, 02:57 PM   #1 (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 C# regex help

Arghhhhh I frigin hate regex!!! Whoever designed regex did it in the most impossible illogically way he could have.

Ok so, I'm enhancing a program I made. The program is to connect to a CoD4 server with a string the user inputs into a text field. The string will look like this:

/connect 1.2.3.4:12345; password blah

This is the correct way to make the string. But I want my program to be able to correct mistakes in the string. For example, say they miss the semi colon after the port, or misspell the word password...etc.

So what I'm trying to do right now is checks to make sure each segment is correctly inputted. I'm having some trouble here, because regex just makes no sense to me.

So here is my test method for testing the regex:

Code:
private void match()
        {
            String input = this.input.Text;

            Regex pattern = new Regex("[0-9].[0-9].[0-9].[0-9]");

            if (input != "")
            {
                if (pattern.IsMatch(input))
                {
                    this.isMatch.Text = "Match";
                }
                else
                {
                    this.isMatch.Text = "Not Match";
                }                
            }
        }
This code is supposed to check to make sure the IP contains only integers, and is formatted like an IP should be (111.222.333.444).

I want this particular regex to do the following:
-Ensure that each segment in the IP is ONLY a number (in my code, you can randomly stick letters in there for some unknown-to-me reason)
-Ensure that each segment in the IP contains only 1-3 numbers
-Ensure that the IP is formatted as such: 111.222.333.444

I won't ask ya'll to write the whole regex portion of this program, but I'm hoping that if you can show me how to do this part that I can pick up on the rest. And yes I've tried going to regular-expressions.com and yeah... it just doesn't make any sense to me. I do what I think is the logical way and it doesn't work.

Sorry for long post, I tend to get carried away.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-31-2009, 08:54 AM   #2 (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

well I can answer some of that right now.
Remember that you've enclosed 0-9 in square brackets [ ]. That specifies to look for only one match. So it looks ONLY for ONE number in each octect, hence why you can slip in some letters too.

This is what I'm thinking right now:
Regex pattern = new Regex("\b[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}\b");

Bear in mind the last time I did Regex was like a year or two ago That might need a little tweaking, but it should set you on the right path.

nehuz, put it in and see what it does. The only part I'm not sure about is how your program will like the { }'s in the regex expression, but like I said it *should* work...
__________________
"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
>>>><<<<

Last edited by S0ULphIRE; 05-31-2009 at 08:56 AM.
S0ULphIRE is offline  
Old 05-31-2009, 11:14 AM   #3 (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

That gives me No Match.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-31-2009, 05:49 PM   #4 (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

JavaScript Lab - Tools - JS Regex Generator
office politics is offline  
Old 05-31-2009, 08:04 PM   #5 (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 office politics View Post
Thanks, but C# isn't liking the code that gives me.
__________________

Need website help? PM me!
CrazeD is offline  
Old 05-31-2009, 08:59 PM   #6 (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

hmmm...ok. I'll write it up myself and test it. I'll try and post back by tonight.

edit: ach, can't do anything on this Mac. I'm missing all my programs. Looks like it'll have to wait until I finish work.
__________________
"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
>>>><<<<

Last edited by S0ULphIRE; 05-31-2009 at 10:17 PM.
S0ULphIRE is offline  
Old 06-01-2009, 06:27 AM   #7 (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

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
__________________
"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
>>>><<<<

Last edited by S0ULphIRE; 06-01-2009 at 06:39 AM.
S0ULphIRE is offline  
Old 06-01-2009, 08:56 AM   #8 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,158

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: C# regex help

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
__________________
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 online now  
Old 06-01-2009, 04:50 PM   #9 (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 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  
Old 06-01-2009, 05:49 PM   #10 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,158

kmote has a spectacular aura aboutkmote has a spectacular aura about

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.



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?
What happened when you had \d and \.? I ask because . is a metacharacter that means basically anything and it needs to be escaped when outside a character class if you want it to be literal. The reason your negated class is not working is because it only checks character 2-4 depending on the previous repetition.
__________________
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 online now  
 
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