pmn
pmn

Reputation: 2247

asterisk and Plus definition issue

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. 

and

The plus tells the engine to attempt to match the preceding token once or more

i googled about meaning of above sentences, but i really can not understand what is meant by the term preceding token once or more and preceding token zero or more times, some one can explain these by an example? any help will be appreciated.

Upvotes: 0

Views: 640

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391326

"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.

Upvotes: 3

Related Questions