What does this ((\d\d\d)\s)? regex match?
-
Why don't you [try it](http://www.regex101.com) ? – Tim Biegeleisen Mar 05 '18 at 05:55
-
Im just confused about the backslash in the beginning – Moses Kirathe Mar 05 '18 at 05:55
-
You have two different expressions in the title and the question. Pick one. – Mad Physicist Mar 05 '18 at 05:56
-
2Possible duplicate of [Meaning of regular expressions like - \\d , \\D, ^ , $ etc](https://stackoverflow.com/questions/36982512/meaning-of-regular-expressions-like-d-d-etc) – Abdulla Nilam Mar 05 '18 at 05:56
-
and https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – Abdulla Nilam Mar 05 '18 at 05:56
-
The regex in the header and description is different, which one are you interested to know. – Hassan Imam Mar 05 '18 at 05:56
-
They are just escaping the literal open/close parentheses. – Tim Biegeleisen Mar 05 '18 at 05:56
-
The one in the header @HassanImam – Moses Kirathe Mar 05 '18 at 05:57
-
[click me](https://regex101.com/r/kDEwvq/1) – guijob Mar 05 '18 at 05:58
-
Another example, \\d. This regex does not accept a single digit, say 5. How different is it from \d? @TimBiegeleisen – Moses Kirathe Mar 05 '18 at 06:01
-
`(\(\d\d\d\)\s)?` means do a non-greedy regex search for 3 digits inside parenthesis followed by a space. – Hassan Imam Mar 05 '18 at 06:03
-
Originally, a backslash always escaped any character, so `\.` matches a literal dot, `\[` matches an opening square bracket, etc. At some point, shorthands like `\t` (tab) were loaned from C, and then the floodgates were open. In Perl-style regex, *generally* a backslash before an alphabetic represents a character class, while backslashes before punctuation quotes it. A backslash before a backslash, of course, also quotes it, so `\\ ` (still) matches a literal backslash. – tripleee Mar 05 '18 at 06:31
4 Answers
\d matches the digits. it is all about the langugae you are using. In python3, [0-9] matches only 0123456789 characters, while \d matches [0-9] and other digit characters, for example Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩.
- \s matches any whitespace character

- 61
- 6
\d matches digits from [0-9].
\s matches white-space characters like [ \t\n\r]
? is means optional, it matches even if the following regex are not present.
() are used for grouping.
Now the question is what does ((\d\d\d)\s)? match? \d\d\d matches 3 consecutive digits and group them to $1.
((\d\d\d)\s) matches 3 consecutive followed by space and this is grouped to $2.
since we have ? at the end of the regex, it matches digits followed with space and also if there are no such match.
In case if there is no match, it points to start of the line.

- 994
- 8
- 19
The first backslash escapes the open parenthesis that follows, as it is a special character, so the regex will search for an open and a close parenthesis in the input string
Example : (111)

- 322
- 1
- 3
- 14
have a look at this site https://regex101.com/r/yS5fU8/2
1st Capturing Group (\d\d\d)
- p (\d\d\d) \d matches a digit (equal to [0-9])
- \d matches a digit (equal to [0-9])
- \d matches a digit (equal to [0-9])
- \d matches a digit (equal to [0-9])
and - \s matches any whitespace character (equal to [\r\n\t\f\v ])

- 4,145
- 1
- 23
- 22