I would like to be able to use one regex (if possible) to require the string to match [A-Za-z0-9_], but not allow:
[A-Za-z0-9_]
Really
test_0123
t0e1s2t3
0123_test
te0_s1t23
t_t
Invalid
t__t
____
01230123
_0123
_test
_test123
test_
test123_
The purpose of this is to filter out the usernames for the website I'm working on. I came to the rules for certain reasons.
/ . /users/#{id} id , . .
/users/#{id}
id
_test , , , .. _test.example.com
_test.example.com
t__t . .. t__t.example.com
t__t.example.com
, :
/\A(?!_)(?:[a-z0-9]_?)*[a-z](?:_?[a-z0-9])*(?<!_)\z/i
[a-z]
(?!_)
(?<!_)
: , , lookahead/lookbehind - , - ?: , -, ?: , -:
?:
/\A(?:[a-z0-9]_?)*[a-z](?:_?[a-z0-9])*\z/i
.
, , , , , . ? , , . ( , , . , .)
, ; , ; ; .. .
:
/^(?=[^_])([A-Za-z0-9]+_?)*[A-Za-z](_?[A-Za-z0-9]+)*$/
Edit:
. .
"__", :
([A-Za-z]|[0-9][0-9_]*)([A-Za-z0-9]|_[A-Za-z0-9])*
([A-Za-z]|([0-9]+(_[0-9]+)*([A-Za-z|_[A-Za-z])))([A-Za-z0-9]|_[A-Za-z0-9])*
dang, . Telemachus, , , , . .
, , , , . , , , , . :.
x !~ /[^A-Za-z0-9_]|^_|_$|__|^\d+$/
Rails validates_format_of, , , - , , .
(?=.*[a-zA-Z].*)^[A-Za-z0-9](_?[A-Za-z0-9]+)*$
, , , . , , .
^(([a-zA-Z]([^a-zA-Z0-9]?[a-zA-Z0-9])*)|([0-9]([^a-zA-Z0-9]?[a-zA-Z0-9])*[a-zA-Z]+([^a-zA-Z0-9]?[a-zA-Z0-9])*))$
, , [^ a-zA-Z0-9] [],
/^(?![\d_]+$)[A-Za-z0-9]+(?:_[A-Za-z0-9]+)*$/
, , , . lookhhead - (?![\d_]+$) - ( , ), , .
(?![\d_]+$)
[A-Za-z][A-Za-z0-9_]*[A-Za-z]
This will work for your first two rules (since the second rule requires a letter at the beginning and end for the second rule, it automatically requires a letter).
I am not sure that the third rule is possible with regular expressions.
Source: https://habr.com/ru/post/1714546/More articles:Access authorization information in web.config - .netКак вы получаете доступ к элементам управления пользователя на главной странице с страницы asp: content с помощью мастера? - asp.netЯвляется ли это "правильным" решением для того, чтобы заставить ASP.NET MVC и javascript intellisense играть хорошо? - javascriptWill performance increase to Server 2008 R2? - windows-server-2008ASP.NET multi-line prompt in Mozilla Firefox - is this possible? - c #Intersection of children in ActionScript-3 - actionscript-3How can I reference a hash with a variable name? - perlHow does ENFORCED share issues in ASP.NET MVC? - asp.net-mvcdefining specific custom colors that appear in colordialog? - vb.netAdding jQuery HTML? - jqueryAll Articles