C# regex help

Status
Not open for further replies.

CrazeD

Fully Optimized
Messages
3,736
Location
Maine
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. :p
 
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...
 
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.
 
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
 
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
 
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.

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?
 
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.
 
Status
Not open for further replies.
Back
Top Bottom