Regular Expression: +$ VS *$ VS none

后端 未结 4 1188
半阙折子戏
半阙折子戏 2021-02-08 08:54

In regular expressions, what is the difference between ^[a-zA-Z]+$ and ^[a-zA-Z]*$. Also would I be able to not include it at all, either with ^[

4条回答
  •  悲哀的现实
    2021-02-08 09:50

    + means 1 or more * means 0 or more

    So an empty string is found by ^[a-zA-Z]*$, but not by ^[a-zA-Z]+$

    ^[a-zA-Z]$ means EXACTLY one letter in the ranges a-z and A-Z.

    a+ is a, aa, aaa, ..., aaa...aaa, etc

    a* is an empty string, a, aa, aaa, ..., aaa...aaa, etc

    ^a$ is only a

    EDIT: you can also use ^a?$ to find 0 or 1 occurence of a, so either an empty string or a

提交回复
热议问题