星号和加号定义问题 [英] 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.
和
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.
推荐答案
preceding token"的意思就是,前面的标记.
"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+
匹配一个或多个A
,A*
匹配零个或多个A
>s,比如AAAAA
- 字符类:
\d+
匹配一个数字,一次或多次,如123450
- 字符集:
[af]+
匹配从a
到f
的任何字母,一次或多次,如afdbe
- 一组:
(test)+
匹配文本test
一次或多次,如testtesttest
- A single character:
A+
matches one or moreA
s, andA*
matches zero or moreA
s, likeAAAAA
- A character class:
\d+
matches a digit, one ore more times, like123450
- A character set:
[a-f]+
matches any letter froma
tof
, one or more times, likeafdbe
- A group:
(test)+
matches the texttest
one or more times, liketesttesttest
零或..."表示模式也可能不匹配,这里是一个示例模式:123450*6
,这将匹配以下示例:
"zero or ..." means that the pattern may also match nothing, here is an example pattern: 123450*6
, this will match the following examples:
1234506
<-- 这里0
出现一次(大于零)123450006
<-- 这里出现了 3 次(也超过零次)123456
<-- 这里出现零次,如果你使用*
,这是合法的,但如果模式是123450+6,则不是
.
1234506
<-- here the0
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 been123450+6
.
这篇关于星号和加号定义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!