c# regular expressions, allow numbers and letters only not working
Asked Answered
L

2

5

I'm using ASP.NET MVC.

I need a regular expression that allows only numbers and letters, not spaces or ",.;:~^" anything like that. Plain numbers and letters.

Another thing: 2 characters can't repeat consecutively.

So I can have 123123 but not 1123456.

I got as far as to:

Regex ER1 = new Regex(@"(.)\\1", RegexOptions.None);

Regex ER2 = new Regex(@"[A-Z0-9]", RegexOptions.IgnoreCase);

I could not make it all in one expression and I still have some characters passing through.

Here is my entire code for testing:

class Program
{
    static void Main(string[] args)
    {
        string input = Console.ReadLine();

        Regex ER1 = new Regex(@"(.)\\1", RegexOptions.None);

        Regex ER2 = new Regex(@"[A-Z0-9]", RegexOptions.IgnoreCase);

        if (!ER1.IsMatch(input) && ER2.IsMatch(input))
            Console.WriteLine( "Casou");
        else
            Console.WriteLine( "Não casou");

            Console.ReadLine();
    }
}

I find these expressions quite complex and I'd be really happy to have some help with this.

Largehearted answered 7/2, 2013 at 18:19 Comment(2)
It sounds like your making evil password restrictions. Don't do that.Vowell
Well, I actually have no idea how the code is going to be used. My boss needs it. I do it. Simple as that :D But if I were to guess now that you mentioned it, I kinda think that's what he needs it for. LOL how did you know man?Largehearted
C
11

Let's try this:

@"^(([0-9A-Z])(?!\2))*$"

Explained:

^               start of string
 (              group #1
   ([0-9A-Z])   a digit or a letter (group #2)
   (?!\2)      not followed by what is captured by second group ([0-9A-Z])
 )*             any number of these
$               end of string

 

The ?! group is called a negative lookahead assertion.

 

(LastCoder's expression is equivalent)

Carleencarlen answered 7/2, 2013 at 18:23 Comment(7)
You have an extra backslashVowell
Oops, started with an ordinary stringCarleencarlen
Yours didn't work either man. Regex ER = new Regex(@"^(([0-9A-Z])(?!\\2))*$", RegexOptions.None); Does not let me type letters. But thanks anywaysLargehearted
Oh man. OK. This one works perfectly now. As far as I can tell. Thank you so much. I guess I'm gonna earn a golden star at work now hahahaha. Thank you all for your efforts, time and help. Regards from Brazil. Cesar. ;)Largehearted
I'd like to ask for an extra help. How can I use that expression in an attribute? Such as: [RegularExpression(@"^(([0-9A-Z])(?!\2))*$"]Largehearted
Sorry, I'm here for the regexps, not the C#. You do have unbalanced parentheses though in your comment: [RegularExpression(@"^(([0-9A-Z])(?!\2))*$")]Carleencarlen
Yeah. That's OK. I just removed the rest of the C# code there and took away the other parentheses. Thank you for your help anyways. =)Largehearted
U
2

Something like this should work

@"^(?:([A-Z0-9])(?!\1))*$"
Unrest answered 7/2, 2013 at 18:25 Comment(3)
I tested it this way and it didn't work: I don't know if I'm doing something wrong. static void Main(string[] args) { string input = Console.ReadLine(); Regex ER = new Regex(@"^(?:([A-Z0-9])(?!\1))*$", RegexOptions.None); if (ER.IsMatch(input)) Console.WriteLine( "Casou"); else Console.WriteLine( "Não casou"); Console.ReadLine(); } It does not accepts letter this way. Only numbers. But dots and comas are also blocked.Largehearted
Oh. Ok. I got that. Now this is almost perfect. The only thing missing is the "no-repeat" thing. For example. If I type 123123 it should pass, but not if I write 1233. It should not let me repeat 2 characters consecutively.Largehearted
Your answer works perfectly in the example I gave above. But I could not make it work as an attribute. Why is that so? [Required(ErrorMessage = "Required")] [RegularExpression(@"^(([0-9A-Z])(?!\2))*$", ErrorMessage = "Required")]Largehearted

© 2022 - 2024 — McMap. All rights reserved.