How to split this string in java need regex?

Luca Chiartosini

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/
Scott Weaver

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split a java string with a regex?

From Dev

What regex do I need for Java String.split for this case

From Dev

How to split string by java regex with look behind?

From Dev

How to split string using regex in java

From Dev

Java string split() Regex

From Dev

Java string split() Regex

From Dev

I need Split String in java?

From Dev

java - using regex to split string

From Dev

Java regex split string not working

From Dev

Regex based string split in Java

From Dev

java - using regex to split string

From Dev

How to split a String using regex in Java without losing numberic

From Dev

How to split String without regex

From Dev

How to split String without regex

From Dev

How to split a string on regex in Python

From Dev

How to regex split, but keep the split string?

From Java

How to split a string in Java

From Dev

How to split the string in java by \?

From Dev

I need How to split this string in javascript?

From Dev

Regex - I need to split a string after the first number

From Dev

Java Caching String.split Regex

From Dev

Java regex to split string with variable spaces and delimiter

From Dev

Java regex split string after number

From Dev

Java String split with regex ignoring content in parenthesis

From Dev

Java Regex to split a String with int, doubles

From Dev

java split string with regex and store it in variables

From Dev

Does not work Regex for multiple string split in java?

From Dev

Java regex split String into an array of numbers

From Dev

Split string array in Java using regex