1

i need to split this string:

COMITATO: TRIESTE Indirizzo legale: VIA REVOLTELLA 39 34139 
Trieste (Trieste) Mob.: 3484503368 Fax: 040310096 Sito web: www.csentrieste.it/

the wanted result must be an array like:

{COMITATO:,TRIESTE,Indirizzo legale:,VIA REVOLTELLA 39 34139 
Trieste (Trieste) ,Mob.:,3484503368,Fax:,Sito web:,www.csentrieste.it/}

the problem is also that some attribute of string can be missing so i cant split using the header of attribute like "COMITATO:" or "Indirizzo legale:"

example:if "Indirizzo legale:" its missing string will appear like:

COMITATO: TRIESTE Mob.: 3484503368 Fax: 040310096 Sito web: www.csentrieste.it/
5
  • first split by a space followed by the word followed by :, then split each by a space, setting limit to 2 items. What did you try so far? Commented Apr 15, 2016 at 15:21
  • can't use the word for split cause it possible some words change and are not present. Commented Apr 15, 2016 at 15:27
  • I mean not particular word, but any word. Are you familiar with lookaheads? They can be used in String#split() as well Commented Apr 15, 2016 at 15:29
  • i know im looking exactly for the right regex sintax , still the problem that some words have space but must not be divided like "VIA REVOLTELLA 39 34139 Trieste (Trieste) " must be 1 word Commented Apr 15, 2016 at 15:35
  • I do not offer split by word boundaries, I offer to split by a space followed by a word followed by a colon. This condition can be easily built using lookahead. Commented Apr 15, 2016 at 15:41

1 Answer 1

1

Well, this regex will parse your given inputs:

(?<firstname>.*?):\s*(?<lastname>\w+)(?:(?<occupation>[^:]+):\s*(?<address>.+\n.+))?\sMob.:\s*(?<mobile>\d+)\s*Fax:\s*(?<fax>\d+)\s*Sito web:\s*(?<website>.*)

We can salvage some readability and easy access of the results by using named groups. Nothing too clever about the regex, we just crawl through the string, using what static structure we can to anchor the pattern: the colons, the "Mob", "Fax", and "Sito web". Obviously the "maybe missing" address part is optional.

regex demo here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.