Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...