-3

how can I do a regex that follow the exact format below? With spaces, signs, brackets, everything?

+44 (0)20 1234 5678

These would also need to work:

+44 (0)79 1234 567
+44 (0)79 1234 5678
+44 (0)79 1234 56789
+44 (0)79 1234 567810
Muppet
  • 5,767
  • 6
  • 29
  • 39
  • 1
    A question is considered good once you represent your efforts on solving it. – revo Feb 19 '18 at 20:13
  • This is not a code/SQL/regex writing service, where you post a list of your requirements and language of choice and a code monkey churns out code for you. We're more than happy to help, but we expect you to make an effort to solve the problem yourself first. Once you've done so, you can explain the problem you're having, include the **relevant** portions of your work, and ask a specific question, and we'll try to help. Good luck. – Ken White Feb 19 '18 at 20:15
  • See the question asked on stack overflow. https://stackoverflow.com/questions/11518035/regular-expression-for-uk-based-and-only-numeric-phone-number-in-cakephp – Raj Thakkar Feb 19 '18 at 20:16
  • 1
    You mean something like [this?](https://regex101.com/r/P3BG8j/1) – Srdjan M. Feb 19 '18 at 20:24
  • It's perfect, thanks S.Jovan! – Muppet Feb 19 '18 at 21:31

1 Answers1

2

I give you some suggestion that will make you able to solve the problem. Then you can try to build your own regex with RegExr.

+, (, and ) have special meaning in a regex, so they need to be escaped with \+, \( and \). A digit can be matched with [0-9] or simply \d.

If you want to match x twice, you can use the syntax x{2}, if a number of times between 2 and 50 you can use x{2,50}. A single space can be matched by .

Easy enough?

Nisba
  • 3,210
  • 2
  • 27
  • 46