0

I'd like to separate the data input by my user on their shipping information into the respective field. The data input will be in this format:

Name - Phone Number - Address

For Example:

Andy Leblanc    (0123-123-12312)   Seaview Av. Street no 21, East Singapore 11221

Name could be any characters, phone would be number with these characters possibly included: "()[]./- ", and address would again be any character.

So the 2 characters field are separated by a number field. Is this possible to separate using regex?

Mariano
  • 6,423
  • 4
  • 31
  • 47
DiMa
  • 35
  • 1
  • 6
  • 1
    Do you expect any addresses to start with a number (would be common in the UK for instance) - may be tricky to tell the difference between end of phone number and start of address if so. – James Thorpe Nov 12 '15 at 09:48
  • It is not recommended to use regex for addresses https://smartystreets.com/articles/regular-expressions-for-street-addresses – camiblanch Dec 30 '15 at 20:31

1 Answers1

1

I came up with the following solution.

My search regex is:

([\w\s]+)\s([\(\)\d-\[\]\.\/-]+)\s(.*)

My replacement string is:

Name:\1#Phone:\2#Address\3

Edit: Included more characters in the phone section.

Yaron
  • 1,199
  • 1
  • 15
  • 35
  • OP said the phone number may contain spaces, which pushes the end of the number into the start of the address in this solution. I'm unsure that given the requirements that this will have a sensible way to solve it. – James Thorpe Nov 12 '15 at 10:02
  • No change since the space after the phone number is declared: https://regex101.com/r/pK9pT1/3 – Yaron Nov 12 '15 at 11:29
  • But now the address can't start with digits (they get interpreted as part of the phone number), which is the norm here in the UK - OP needs to clarify his situation. – James Thorpe Nov 12 '15 at 11:32
  • You can have an address starting with a number, just click the link I posted earlier and try, the space is still a separator regardless of character type. – Yaron Nov 12 '15 at 11:36
  • That's the problem - the first one interprets it as part of the address, the second one as part of the phone number. You can't have it both ways, eg in "James Thorpe 01888 435 123 92 Some Road, Some Place", is the 92 part of the phone number or the address? It's an impossible question – James Thorpe Nov 12 '15 at 11:37
  • You are correct but as long as we keep a certain standard we can do it, another suggestion could be adding some sort of separator to distinguish the fields. – Yaron Nov 12 '15 at 11:41
  • Indeed. Hence my comment on the question attempting to prompt the OP for more information, but it appears to have been an "ask and run". – James Thorpe Nov 12 '15 at 11:42
  • @JamesThorpe Its not the norm where I live to have number in the front of the address, but I may have to enforce certain format to my users to include this possibility – DiMa Nov 14 '15 at 01:18
  • in case of the example given by Thorpe : James Thorpe 01888 435 123 92 Some Road, Some Place, some part of the phone number got included as the name part using this regex, can this be solved if the end of the name part can never be a number? Similarly maybe I could enforce that the beginning of the address part can never be number – DiMa Nov 14 '15 at 03:56
  • I added to the regex so that it matched Thorpe example, is this right ? `([\w\s]+[^\d])\s([\s\(\)\d-\[\]\.\/-]+)\s(.*)` – DiMa Nov 14 '15 at 04:24