I am trying to figure out how to use positive lookahead to get this to work with no success. I am playing with something similar to:
^(?=.*[a-zA-Z0-9])(\w{6})$
The only requirements are that
1. the string is 6 characters
2. has exactly 2 alphabetic and 4 digits.
So positive matches would be:
A0123A
AA0123
0123AA
A01A23
Your ^(?=.*[a-zA-Z0-9])(\\w6)$
matches a string that has at least 1 ASCII letter or digit (due to (?=.*[a-zA-Z0-9])
) and only has 1 word character followed with a 6
symbol. So, it will match strings like 16
or _6
, or a6
. Thus, it will not work as you expect.
You may use
^(?=.{6}$)\d*(?:\p{L}\d*){2}$
See the regex demo
Explanation :
^
- start of string (?=.{6}$)
- a check that makes sure there are 6 characters in the string \\d*
- zero or more digits (?:\\p{L}\\d*){2}
- 2 sequences of a letter and 0+ digits $
- end of string If you need to limit the expression to matching ASCII letters and digits, replace \\p{L}
with [a-zA-Z]
and \\d
with [0-9]
.
Also, to make sure you match at the start and the very end of the string, use \\z
anchor instead of $
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.