\d
matches a digit (Does "\d" in regex mean a digit?)
\b
marks the end of a word/number
[0-9]{13-16}
indicates that the repetition of the characters in the brackets before, either 13,14,15 or 16 times. The background is that old credit cards numbers have 13 digits newer credit card numbers have 16 digits.
So \b\d{13,16}\b
will find/match any sequence of 13 to 16 digits, meaning that it can be used to find credit card numbers without any '-'
?:
is another special case meaning 'clustering without capturing` (use of colon symbol in regular expression)
(?:pattern)
is used to match the pattern, but not capture it resulting in removing the questioned characters from the result string, i.e. the '-' (What is a non-capturing group? What does a question mark followed by a colon (?:) mean?)
*
means zero or more repetitions of the character(s) in the element before
?
means the characters in the element before can appear, but don't have to
*?
(used in the regex above) is the non-greedy / lazy version i.e. [^a]*?
means "a sequence of 0 or more characters, not containing 'a', as short as possible while conforming to the rest of the regular expression."
So the regex matches any sequence of numbers of length 13 to 16 containing an arbitrarily number of '-' and ' ' (whitespaces)
Note that the non-capturing group ?:
matches but does not capture the substring in the result: The regex \b(a)(?:b)(c)\b
applied on the string "abc"
matches but in the result the non-capturing group is skipped ( a group is everything in ( )
):
match: "abc", match1: "a", match2: "c"
("b" does not appear in the match list)
However the entire match can be retrieved by calling group()
on the match
object, see Alan Moore's comment below.
You can test this on https://regex101.com/
See this Regex credit card number tests