Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends,

I'm trying to create a regular expression in C# for the user name. My requirement is:

- User name can be of one word or of two words with exactly one space or dot or underscore character between them e.g. code project, code.project, code_project. more than one of any of these character is not allowed.

- It should not accept any other special characters.

I created this regex: @"^([aA0-zZ9]*[\s\.\_]?[aA0-zZ9]*)$"

But this is accepting more than one UNDERSCORE and @ (one or more than one) character by default. I don't want it to accept exactly one underscore character b/w two words. also @ should not be allowed. I have not included it in Regex. where does it came from???

Any help would be great.

Thanks in Advance,
Gobind
Posted

Try this:
^[a-zA-Z0-9]+([._\s][a-zA-Z0-9]+)?$
 
Share this answer
 
I don't want to write complete regular expression for you, I'll just show your mistake. Too many characters are allowed due to this part: [aA0-zZ9]*. In this expression, the range 0 to z is present; this is what gives you all your problems. You want to allow just the union of the ranges 0-9, a-z and A-Z, right? This is written in the following way:
[a-zA-Z0-9]*.

I think if you fix it, you will easily get what you want — you are almost there.

—SA
 
Share this answer
 
Comments
gobind_786 6-Aug-14 14:06pm    
Thanks Sergey, It fixed my issue.
Sergey Alexandrovich Kryukov 6-Aug-14 14:24pm    
Great!
Good luck, call again.
—SA
[aA0-zZ9] means a or A or anything from 0 to z or Z or 9 (see below).
You probably mean [a-zA-Z0-9] .
The [\s\.\_] part might be better as [ ._] . And to be clear when you say "space" do you mean "any whitespace" or only an actual SPACE?


Have a look at RegexTester[^], and also follow the link to Expresso.

MSDN: http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx[^]

"anything from 0 to z"

 48 060 30 00110000 0
 49 061 31 00110001 1
 50 062 32 00110010 2
 51 063 33 00110011 3
 52 064 34 00110100 4
 53 065 35 00110101 5
 54 066 36 00110110 6
 55 067 37 00110111 7
 56 070 38 00111000 8
 57 071 39 00111001 9
 58 072 3A 00111010 :
 59 073 3B 00111011 ;
 60 074 3C 00111100 <
 61 075 3D 00111101 =
 62 076 3E 00111110 >
 63 077 3F 00111111 ?
 64 100 40 01000000 @   <---
 65 101 41 01000001 A
 66 102 42 01000010 B
 67 103 43 01000011 C
 68 104 44 01000100 D
 69 105 45 01000101 E
 70 106 46 01000110 F
 71 107 47 01000111 G
 72 110 48 01001000 H
 73 111 49 01001001 I
 74 112 4A 01001010 J
 75 113 4B 01001011 K
 76 114 4C 01001100 L
 77 115 4D 01001101 M
 78 116 4E 01001110 N
 79 117 4F 01001111 O
 80 120 50 01010000 P
 81 121 51 01010001 Q
 82 122 52 01010010 R
 83 123 53 01010011 S
 84 124 54 01010100 T
 85 125 55 01010101 U
 86 126 56 01010110 V
 87 127 57 01010111 W
 88 130 58 01011000 X
 89 131 59 01011001 Y
 90 132 5A 01011010 Z
 91 133 5B 01011011 [
 92 134 5C 01011100 \
 93 135 5D 01011101 ]
 94 136 5E 01011110 ^
 95 137 5F 01011111 _
 96 140 60 01100000 `
 97 141 61 01100001 a
 98 142 62 01100010 b
 99 143 63 01100011 c
100 144 64 01100100 d
101 145 65 01100101 e
102 146 66 01100110 f
103 147 67 01100111 g
104 150 68 01101000 h
105 151 69 01101001 i
106 152 6A 01101010 j
107 153 6B 01101011 k
108 154 6C 01101100 l
109 155 6D 01101101 m
110 156 6E 01101110 n
111 157 6F 01101111 o
112 160 70 01110000 p
113 161 71 01110001 q
114 162 72 01110010 r
115 163 73 01110011 s
116 164 74 01110100 t
117 165 75 01110101 u
118 166 76 01110110 v
119 167 77 01110111 w
120 170 78 01111000 x
121 171 79 01111001 y
122 172 7A 01111010 z
 
Share this answer
 
v4
Comments
gobind_786 6-Aug-14 14:09pm    
Thanks PIEBALDconsult, 0 to z range makes it clear all the characters allowed in this range. quite helpful.
You can try this
C#
^([a-zA-Z0-9]*[\s._]?[a-zA-Z0-9]*)$

You can test your regex at http://regex101.com/[^]
 
Share this answer
 
v4
Comments
PIEBALDconsult 6-Aug-14 0:38am    
You don't want parentheses inside the brackets.

Unrecognized escape sequence \_.

That site doesn't appear to have a .net setting.
Kumarbs 6-Aug-14 1:07am    
Thank you for your inputs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900