admin管理员组文章数量:1394593
I'm trying to create a Regex. The rules are:
1) Only letters (both upper and lower), numbers, periods (.) and underscores (_)
2) Must begin and end with letter or number (i.e. no periods/underscores)
3) Cannot have multiple punctuations in a row, of any kind (no .. or ._ or _. or __ or .._. etc)
4) Must contain at least 2 letters (either upper or lower or both, doesn't matter)
5) No less than 3 chars, no more than 25 chars
So far, what I have is:
^((?!.*[\._]{2})[A-Za-z0-9\._]{3,25})$
Which is close-ish. I've got rules 1, 3 and 5, but cannot figure out 2 and 4.
I thought that
(([A-Za-z0-9]?)([A-Za-z0-9\._]+)([A-Za-z0-9]?)){3,25}
would get rule 2, but it just breaks it.
I've been working on this for a couple hours, but none of the solutions I can find, or can e up with, are quite what I'm looking for.
An explanation of the Regex statement you provide would be greatly appreciated, but is not entirely necessary.
EDIT 1
As per noob's suggestion:
^((?!.*[\._]{2})(([\w\d\.]+)){3,25})$
EDIT 2
Valid:
1rockit.man
q3w
e4.45r.d.2
sp_am
(and anything else between 3 and 25 characters, without leading or repeating punctuation)
Invalid:
.ret.
123
12a
ret..wer
super_.duper
_petrat
petrat_
I'm trying to create a Regex. The rules are:
1) Only letters (both upper and lower), numbers, periods (.) and underscores (_)
2) Must begin and end with letter or number (i.e. no periods/underscores)
3) Cannot have multiple punctuations in a row, of any kind (no .. or ._ or _. or __ or .._. etc)
4) Must contain at least 2 letters (either upper or lower or both, doesn't matter)
5) No less than 3 chars, no more than 25 chars
So far, what I have is:
^((?!.*[\._]{2})[A-Za-z0-9\._]{3,25})$
Which is close-ish. I've got rules 1, 3 and 5, but cannot figure out 2 and 4.
I thought that
(([A-Za-z0-9]?)([A-Za-z0-9\._]+)([A-Za-z0-9]?)){3,25}
would get rule 2, but it just breaks it.
I've been working on this for a couple hours, but none of the solutions I can find, or can e up with, are quite what I'm looking for.
An explanation of the Regex statement you provide would be greatly appreciated, but is not entirely necessary.
EDIT 1
As per noob's suggestion:
^((?!.*[\._]{2})(([\w\d\.]+)){3,25})$
EDIT 2
Valid:
1rockit.man
q3w
e4.45r.d.2
sp_am
(and anything else between 3 and 25 characters, without leading or repeating punctuation)
Invalid:
.ret.
123
12a
ret..wer
super_.duper
_petrat
petrat_
Share
Improve this question
edited Feb 27, 2016 at 8:26
Aprillion
22.4k6 gold badges58 silver badges91 bronze badges
asked Feb 27, 2016 at 6:13
BirrelBirrel
4,8347 gold badges41 silver badges79 bronze badges
5
-
1
Use
shorthand
operators like\w
and\d
. Easier to prehend.\w
also include_
so that's covered. – user2705585 Commented Feb 27, 2016 at 6:17 - You might want to include a few valid and invalid matches for quick testing. – user2705585 Commented Feb 27, 2016 at 6:27
-
1rockit.man
ande4.45r.d.2
are invalid according to condition 2. Cannot begin or end with non-letter/number. You might wanna check again the given condition or the example. – user2705585 Commented Feb 27, 2016 at 6:37 - @noob cannot begin with non-letter or non-number. i.e., must begin and end with either letter or number. I'll make it more clear in the post – Birrel Commented Feb 27, 2016 at 6:40
-
Okay.
non-letter/number
seems to be confusing. – user2705585 Commented Feb 27, 2016 at 6:40
4 Answers
Reset to default 7Based upon your conditions I came up with following regex. It's only modification over your current regex.
Regex: ^(?=.*[A-Za-z].*[A-Za-z])([A-Za-z0-9]((?!.*[\._]{2})([\w\d\.]{1,23}))[A-Za-z0-9])$
Explanation:
The first part
(?=.*[A-Za-z].*[A-Za-z])
checks for whole string to havetwo
characters separated by anything (that's pretty much the case here).I added checks on the beginning and end of string which should be either letter or number.
Apart from that the length will also vary now as first and last character is counted rest of the length will be only 23 characters.
Regex101 Demo
Building the regex from the rules (in an order of convenience):
- both upper and lower case => use case insensitive flag
//i
- letters, numbers, underscore and period =>
[\w.]*
- 3-25 characters =>
^[\w.]{3,25}$
- cannot begin or end with period or underscore =>
(?!^[\W_]|[W_]$)
(notice uppercase\W
meaningnot \w
) - cannot contain 2 punctuation in a row =>
(?![\W_]{2})
- checking the negative lookahead in every position =>
(?:(?!...)[\w.]){3,25}
(using non-capturing group(?:)
instead of()
because we don't need to save the group) - at least 2 letters =>
(?:.*[a-z]){2}
assumingi
flag - wrap into lookahead (not consuming any characters) so we can check multiple conditions =>
(?=(?:...))
The final regex literal:
/^(?=(?:.*[a-z]){2})(?:(?!^[\W_]|[\W_]{2}|[\W_]$)[\w.]){3,25}$/i
https://regex101./r/yK1cB3/5#javascript
FTR: in languages that support the
x
flag (ignoring whitespace inside the regex), you could make it more readable (see a workaround for javascript):
^
(?=
(?:.*[a-z]){2}
)
(?:
(?!^[\W_]|[\W_]{2}|[\W_]$)
[\w.]
){3,25}
$
You can try with:
^(?=.*[a-zA-Z].*[a-zA-Z].*)[a-zA-Z0-9](?!.*[._]{2,}|.*[._]$)[._a-zA-Z0-9]{2,24}$
or, if you prefer make it using character classes like \w \d \s, you could use:
^(?=.*[a-zA-Z].*[a-zA-Z].*)\w(?!.*[._]{2,}|.*[._]$)(\w|[._]){2,24}$
http://regexr./3ct34
Takin part in ur riddle challenge. Here's my version. Would have nice performance as well :)
/^(?=(?:.*?[a-z]){2})[a-z\d](?!.*?[._]{2})[\w.]{1,23}[a-z\d]$/i
i
flag for caseless matching where [a-z]
is equivalent with alpha [a-zA-Z]
.
\w
is a short for word character [A-Za-z0-9_]
thus [\w.]
will match allowed characters.
(?:
opens a non capturing group mostly used for repitition or alternation (if group not needed).
(?=
(?!
lookarounds are zero-length assertions triggered at a certain position.
- Rule 2: Require one alnum
[a-z\d]
at^
start and one at$
end - Rule 1 & 5: No less than 3, no more than 25 with start/end substracted
[\w.]{1,23}
- Rule 3:
(?!.*?[._]{2})
no consecutive punctuation checked by negative lookahead. - Rule 4:
(?=(?:.*?[a-z]){2})
at least 2 letters checked with positive lookahead.
Check out demo at regex101
本文标签: JavaScript RegexCannot startend with punctuationno repeating punctuationStack Overflow
版权声明:本文标题:JavaScript: Regex - Cannot startend with punctuation, no repeating punctuation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744095784a2590257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论