判斷輸入字串是否符合「一或多位數字後接單一字母 a」模式,如 `1a`, `20a`, `123a` 合法;`a`, `1b`, `1aa` 不合法。
^\d+a$
boolean matches = str.matches("\\d+a");
const regex = /^\d+a$/;
regex.test("20a"); // true
bool ok = System.Text.RegularExpressions.Regex.IsMatch(str, @"^\d+a$");
import re
re.fullmatch(r'\d+a', s) is not None
| 符號 | 含義 |
|---|---|
| `^` | 字串開頭 |
| `\d+` | 一或多個數字 (0-9) |
| `a` | 字面字元 'a' |
| `$` | 字串結尾 |
^\s*\d+a\s*$
正則表達式是處理此類格式驗證的標準工具,簡潔、可讀、跨語言通用。建議優先使用,除非效能極度敏感(如每秒百萬次),才考慮手寫狀態機。