Reputation: 39
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/
Upvotes: 1
Views: 63
Reputation: 7361
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.
Upvotes: 1