-2

((\d\d\d)\s)?\d\d\d-\d\d\d\d

just wondering what would be the meaning of above?

I think the first part ((\d\d\d)\s)? means something like this:

352k (since \d means random digit, \s means a non-space character, so I just use k here, and ? here means 352k repeated 0 or 1 time, so I have it 1 time here)

Then I am not quite sure what is the meaning of the thing that follows it i.e. \d\d\d-\d\d\d\d
I don't get the - (the minus sign) here and how it relates the first 3 digits, and last 3 digits in this expression here.

P.S. I tried this here (they don't work): (345) 578-3291 (382) 441-2219 or 341 319-3183 or 321 999-1318

so phone number pattern doesn't work. Does anyone know?

john_w
  • 545
  • 1
  • 4
  • 19
  • 1
    `\s` is a space, not a non-space. `-` is not special. It's just a literal `-` – Mat Nov 08 '20 at 23:02
  • I think it would be immensely helpful to just copy-paste it into Regex101 UI, and look at the explanations for all the elements. – Andrey Tyukin Nov 08 '20 at 23:03
  • `\d\d\d-\d\d\d\d` matches `123-4567` – VLAZ Nov 08 '20 at 23:05
  • @Mat do you think it is something like a phone number like this: (345) 578-3291 – john_w Nov 08 '20 at 23:05
  • but (345) 578-3291 doesn't give an answer that matches the expression above – john_w Nov 08 '20 at 23:06
  • The regex doesn't match a literal `(` or `)` character. Those are simple groups. – VLAZ Nov 08 '20 at 23:07
  • but even without the open and closing parenthesis, it still doesn't work, for example the following doesn't work: 341 319-3183 – john_w Nov 08 '20 at 23:09
  • "*for example the following doesn't work: 341 319-3183*" [it doesn't?](https://regex101.com/r/ZkINWa/1) – VLAZ Nov 08 '20 at 23:12
  • @john_w it very much depends on which flavour of regex you're using. A perl-compatible regex (PCRE) would match your input string, whereas a GNU regex would not. – Joseph Thomas-Kerr Nov 08 '20 at 23:14
  • @VLAZ for some reasons 341 319-3183 doesn't work – john_w Nov 09 '20 at 00:02
  • @john_w I've shown you that it does work on Regex101. – VLAZ Nov 09 '20 at 05:55
  • @VLAZ Actually the thing with bracket actually works: (382) 938-1635 . Maybe there was a mistake when I reload the browser. So the phone number pattern actually works. Sorry all for the confusion I created. Also thanks a lot for all the response. – john_w Nov 09 '20 at 06:00

1 Answers1

-1

You should find the answers you're looking for in an introduction to regular expressions, such as https://medium.com/better-programming/introduction-to-regex-8c18abdd4f70. If you want to fiddle with the expression in order to get a better understanding of how it works there are numerous online regex testers (eg https://www.regextester.com/).

One important note is that regexes have a long history and come in different flavours, which often have incompatible syntax.

A rough outline of the regex you have quoted is: - An optional 3 digits and space, followed by - 3 more digits, then - a hyphen character, then - then 4 more digits.

The brackets have the function of grouping the optional section, as well as allowing you to extract the resulting substrings, so if you applied this regex to the string 123 456-7890 then group 1 would contain 123 and group 2 would contain 123. (Group 0 always contains the whole matched string). Alternatively, regex would also match 456-7890 and in this case the capture groups would both be empty.