数字,字母和:的正则表达式

我正在尝试为特定字符串编写一个正则表达式:要匹配的每个字符串应从数字[1-9]开始,并且可能包含或可能不包含前缀。


例如:


0  - not match

1  - match

9  - match 

2: - not match

3:ABC - match

4:56ARD20 - match

5:56ARD20(any other chars except [0-9A-Z]) - not match


A:5GTS - not match (just a digits in the first part)

A1:GRT - not match (just a digits in the first part)


:FDE3  - not match (first part should contain only digits)

:      - not match (empty first digital part)

因此,字符串的第一部分->仅是数字(强制性)。字符串可以包含一个带后缀[0-9A-Z]的符号(:)。


宝慕林4294392
浏览 107回答 2
2回答

一只斗牛犬

正则表达式 ^[1-9]\d*(?::[A-Z\d]+)?$可读的 ^                    # BOS [1-9] \d*            # Digit(s) required (can only start with 1-9 (?:                  # Optional group      :                    # Colon      [A-Z\d]+             # Upper case letters or digits )? $                    # EOS
打开App,查看更多内容
随时随地看视频慕课网APP