![](/img/trans.png)
[英]Regex for first two character of string alphabet and rest should be numeric
[英]Regex for first two character of string alphabet and last two should be numeric
我想驗證一個字符串
1.AB97CD11
案例
我試過這個正則表達式,但它對我不起作用:
^[a-zA-Z]{2}[a-zA-Z0-9]{4}[0-9]{2}$
嘗試以下模式:
^[A-Z]{2}[A-Z0-9]{0,4}[0-9]{2}$
中間字符上的{0,4}
寬度定界符確保總長度必須在 4 到 8 個字符之間。 我假設您只需要大寫字母。 如果字母也可以是小寫,則使用[A-Za-z]
而不是[az]
。
所以我猜你希望同時滿足所有 3 個條件。
您想使用量詞來指定字母/數字的數量。
[a-zA-Z]{2}[\\w]{0,4}[0-9]{2}
會做的工作。
Match a single character present in the list below [a-zA-Z]{2}
{2} Quantifier — Matches exactly 2 times
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Match a single character present in the list below [\w]{0,4}
{0,4} Quantifier — Matches between 0 and 4 times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equal to [a-zA-Z0-9_])
Match a single character present in the list below [0-9]{2}
{2} Quantifier — Matches exactly 2 times
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.