This question already has an answer here:
这个问题已经有了答案:
- What is a non-capturing group? What does (?:) do? 13 answers
- 什么是非捕获组?(?)做什么?13个答案
- Regex: Question mark and colon [duplicate] 2 answers
- 问号和冒号[重复]2个答案。
I have just learned regular expression in my python class and I need help in understanding a chunk of code in Regex. The code is a common credit card search question:
我刚刚在python类中学习了正则表达式,需要帮助理解Regex中的一段代码。代码是常见的信用卡搜索问题:
\b4[0-9]{12}(?:[0-9]{3})\b
I am just wondering what the last portion means:
我想知道最后的部分是什么意思
(?:[0-9]{3})\b
I read from another source that it is grouping assertion, but can anyone please explain it to me more clearly?
我从另一个来源读到它是分组断言,但是谁能更清楚地向我解释一下?
Additionally, what does the \b at the end signify?
另外,末端的\b是什么意思?
Thank you very much
非常感谢你
3 个解决方案
#1
0
You use parentheses to identify a group in the match.
在匹配中使用括号来标识一个组。
?:
marks a non capturing group
?:标记一个非捕获组。
>>> s = '4123412341234123'
>>> print (re.match(r"\b4[0-9]{12}(?:[0-9]{3})\b", s).groups())
()
>>> print (re.match(r"\b4[0-9]{12}([0-9]{3})\b", s).groups())
('123',)
\b
matches a word boundary character
\b匹配一个单词边界字符。
#2
1
It is called non-capturing group. That means the group followed by ?: is optional since VISA cards are between 16 to 13 digits long depending if it's a new card or a long card. This adds 3 optional digits that may or may not be included in the search.
它被称为非捕获组。这意味着该组织紧随其后?:这是可选的,因为VISA卡的长度在16到13位数之间,这取决于它是一张新卡还是一张长卡。这增加了3个可选的数字,可能或不包括在搜索中。
As for the \b at the end, you can think of it as a whole word search in a quote.
至于最后的\b,你可以把它看作是一个完整的单词搜索。
Check this website for more information: http://www.regular-expressions.info/creditcard.html
查看这个网站了解更多信息:http://www.regular-expressions.info/creditcard.html。
Cheers!
干杯!
#3
0
(?:)
denotes non-capturing
group. It means that do not capture the match that follows. If you use ()
(and not (?:)
), it captures whatever is matched in a variable and can be used later as $1
or \1
whichever your language supports. If you use (?:)
, the match will not be stored in any variable.
(?:)表示无群。它的意思是,不要捕捉下面的匹配。如果您使用()(而不是(?:)),它会捕获变量中匹配的任何内容,并且可以在稍后使用您的语言支持的$1或\1。如果使用(?:),匹配将不会存储在任何变量中。
\b
denotes word boundary. It is a position between a word character and non-word character.
\ b表示词界。它是一个字字符和非字字符之间的位置。
(?: #Non-capturing group (Do not store it in a variable)
[0-9] #Match any digit between 0 to 9
{3} #Do this three times (i.e Match any digit between 0 to 9 three times)
)
\b #Word boundary
#1
0
You use parentheses to identify a group in the match.
在匹配中使用括号来标识一个组。
?:
marks a non capturing group
?:标记一个非捕获组。
>>> s = '4123412341234123'
>>> print (re.match(r"\b4[0-9]{12}(?:[0-9]{3})\b", s).groups())
()
>>> print (re.match(r"\b4[0-9]{12}([0-9]{3})\b", s).groups())
('123',)
\b
matches a word boundary character
\b匹配一个单词边界字符。
#2
1
It is called non-capturing group. That means the group followed by ?: is optional since VISA cards are between 16 to 13 digits long depending if it's a new card or a long card. This adds 3 optional digits that may or may not be included in the search.
它被称为非捕获组。这意味着该组织紧随其后?:这是可选的,因为VISA卡的长度在16到13位数之间,这取决于它是一张新卡还是一张长卡。这增加了3个可选的数字,可能或不包括在搜索中。
As for the \b at the end, you can think of it as a whole word search in a quote.
至于最后的\b,你可以把它看作是一个完整的单词搜索。
Check this website for more information: http://www.regular-expressions.info/creditcard.html
查看这个网站了解更多信息:http://www.regular-expressions.info/creditcard.html。
Cheers!
干杯!
#3
0
(?:)
denotes non-capturing
group. It means that do not capture the match that follows. If you use ()
(and not (?:)
), it captures whatever is matched in a variable and can be used later as $1
or \1
whichever your language supports. If you use (?:)
, the match will not be stored in any variable.
(?:)表示无群。它的意思是,不要捕捉下面的匹配。如果您使用()(而不是(?:)),它会捕获变量中匹配的任何内容,并且可以在稍后使用您的语言支持的$1或\1。如果使用(?:),匹配将不会存储在任何变量中。
\b
denotes word boundary. It is a position between a word character and non-word character.
\ b表示词界。它是一个字字符和非字字符之间的位置。
(?: #Non-capturing group (Do not store it in a variable)
[0-9] #Match any digit between 0 to 9
{3} #Do this three times (i.e Match any digit between 0 to 9 three times)
)
\b #Word boundary