asterisk and Plus definition issue

后端 未结 1 1476
难免孤独
难免孤独 2020-12-12 05:54

I start reading Regex, and in article i find these :

The asterisk or star tells the engine to attempt to match the preceding token zero or more times. 


        
相关标签:
1条回答
  • 2020-12-12 06:29

    "preceding token" means just that, the preceding token.

    The effect the * or + has is the rest of the sentence:

    • * means that the preceding token, whatever that may be, can be matched zero or more times
    • + means that the preceding token, whatever that may be, can be matched one or more times

    A "token" here is one unit of pattern, it can be any of the following examples + many more:

    • A single character: A+ matches one or more As, and A* matches zero or more As, like AAAAA
    • A character class: \d+ matches a digit, one ore more times, like 123450
    • A character set: [a-f]+ matches any letter from a to f, one or more times, like afdbe
    • A group: (test)+ matches the text test one or more times, like testtesttest

    "zero or ..." means that the pattern may also match nothing, here is an example pattern: 123450*6, this will match the following examples:

    • 1234506 <-- here the 0 occurs once (which is more than zero)
    • 123450006 <-- here it occurs three times (also more than zero)
    • 123456 <-- here it occurs zero times, which is legal if you use the *, but not if the pattern had been 123450+6.
    0 讨论(0)
提交回复
热议问题