Regular Expression: +$ VS *$ VS none

后端 未结 4 1288
半阙折子戏
半阙折子戏 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:40

    ^[a-zA-Z]+$
    

    [a-zA-Z]+ match a single character present in the list below
    Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    a-z a single character in the range between a and z (case sensitive)
    A-Z a single character in the range between A and Z (case sensitive)
    $ assert position at end of the string

    So this will match basically any string of letters Foo, Bar, sdasndjkasnd
    But will not match anything else like asds2, ab c, @23 Will not match

    ^[a-zA-Z]*S
    

    [a-zA-Z]* match a single character present in the list below
    Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    a-z a single character in the range between a and z (case sensitive)
    A-Z a single character in the range between A and Z (case sensitive)
    S matches the character S literally (case sensitive)

    This will match the same but you MUST have a capital S at the end for a match.

    Such as BallS, FooS, BarS will match -- ASzs will match [AS]
    Balls, Foos Will not match though

提交回复
热议问题