How to validate version number using regex pattern

后端 未结 2 414
南旧
南旧 2021-01-21 22:39

I have to validate version number pattern for following examples:

A1
aabc1
AabC134
aabc12.2
aA1.2.3
0.1.1
0.0.2
a.b.c
a.1.2
a.0.0
1.0.0
1.0
1

B

相关标签:
2条回答
  • 2021-01-21 23:27

    You may use

    /^(?![0.]+$)[A-Za-z0-9]+(?:\.[A-Za-z0-9]+){0,2}$/
    

    Or,

    /^(?![0.]+$)[A-Z\d]+(?:\.[A-Z\d]+){0,2}$/i
    

    See the regex demo

    Details

    • ^ - start of string
    • (?![0.]+$) - no just zeros / dots till the end of string
    • [A-Za-z0-9]+ - one or more digits/letters
    • (?:\.[A-Za-z0-9]+){0,2} - 0, 1 or 2 repetitions of . and 1+ digits or letters
    • $ - end of string

    Regex graph:

    0 讨论(0)
  • 2021-01-21 23:27

    I might just use negative lookaheads to assert that the blacklisted version numbers do not appear, and otherwise proceed along the lines of what you are already doing:

     ^(?!^(?:0|0\.0\.0|000\.000\.000|0000\.00\.00)$)[A-Za-z0-9]+(?:\.[A-Za-z0-9]+){0,2}$
    

    Demo

    0 讨论(0)
提交回复
热议问题