-2

I need a regex that converts +442070320811 to 2070 320811.

Using this regex \(\d{4})(\d{6})(\d*)$ gives me 4420 703208

Can anyone advise how I can start the match after the 44?

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – hsz Jun 13 '14 at 14:02

4 Answers4

0

This should work:

\+\d{2}(\d{4})(\d{6})(\d*)$
Julian Irwin
  • 172
  • 10
0

Search: \+44(\d{4})(\d{6}\b)

Replace: $1 $2 or \1 \2 depending on language / environment.

zx81
  • 41,100
  • 9
  • 89
  • 105
  • Thanks, I should have mentioned this is javascript. – user3737941 Jun 13 '14 at 14:59
  • Glad it worked, hope to see you again. Hey btw, I notice you haven't yet voted on Stack. If this answer helps and for any answer you find helpful, please consider voting up as this is how the rep system works.:) No obligation of course! Thanks for listening to my 10-second SO rep tut. – zx81 Jun 13 '14 at 15:02
0

JavaScript:

text.replace(/(\+44)(\d{4})/g,'$2 ');

Java:

text.replaceAll("(\\+44)(\\d{4})","$2 ")

Simply use regex grouping feature that groups pattern inside parenthesis ().

Online demo on regex101 and debuggex

Braj
  • 46,415
  • 5
  • 60
  • 76
  • This worked perfectly for uk numbers but it turns out there are other country codes too so gradi3nt's solution addressed both fortunately. – user3737941 Jun 13 '14 at 14:58
  • You have not mentioned it in your post. If you did it then I will definitely provide you solution as per your need. – Braj Jun 13 '14 at 14:59
  • where have you mentioned that what language are you using. Is your question is complete in the context of you original issue? – Braj Jun 13 '14 at 15:00
  • You are agree with gradi3nt's solution but still you have accepted other one. Why? – Braj Jun 13 '14 at 15:02
0

This also worked:

^.{2}((\d{4})(\d{6})(\d*)$)

Then I could refer to the sections using $2,$3 and $4

Braj
  • 46,415
  • 5
  • 60
  • 76