Sunday, 15 August 2010

regex - c# regular expressions, allow numbers and letters only not working -



regex - c# regular expressions, allow numbers and letters only not working -

i'm using asp.net mvc.

i need regular look allows numbers , letters, not spaces or ",.;:~^" that. plain numbers , letters.

another thing: 2 characters can't repeat consecutively.

so can have 123123 not 1123456.

i got far to:

regex er1 = new regex(@"(.)\\1", regexoptions.none); regex er2 = new regex(@"[a-z0-9]", regexoptions.ignorecase);

i not create in 1 look , still have characters passing through.

here entire code testing:

class programme { 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 , i'd happy have help this.

let's seek this:

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

explained:

^ start of string ( grouping #1 ([0-9a-z]) digit or letter (group #2) (?!\2) not followed captured sec grouping ([0-9a-z]) )* number of these $ end of string

 

the ?! grouping called negative lookahead assertion.

 

(lastcoder's look equivalent)

c# regex asp.net-mvc expression

No comments:

Post a Comment