簡體   English   中英

字符串字母表的前兩個字符和后兩個字符的正則表達式應該是數字

[英]Regex for first two character of string alphabet and last two should be numeric

我想驗證一個字符串

1.AB97CD11

案例

  1. 字符串總長度 min=4, max=8
  2. 前兩個字符必須是字母
  3. 最后兩個字符必須是數字。

我試過這個正則表達式,但它對我不起作用:

^[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}

會做的工作。

解釋取自https://regex101.com/

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM