Separation of newline characters AND and commas and half-columns

I would like to assign the user names in the text environment entered by the user and split them into elements in the array.

Take the theoretically possible input:

people = "Abby Andrews, Ben \r\nCharlie Connors Daphne D., Ernie E. Engels; Faye\r\n\r\nGary Gomez" array = people.split('??') 

How can I create a regex to successfully split the crazy chain as shown above?

Must be shared:

  • \r , \n , \r\n
  • comma ( , ) or semicolon ( ; )
  • some spaces

It should not be divided:

  • period (it may be someone starting)
  • one space (there may be a separation of the name and surname)

I tried people.split(/\r\n,;/) people.split(/,;\r\n/) people.split(/\r\n,;/) , people.split(/,;\r\n/) and their combinations, but none gave the result.

+4
source share
1 answer

Try

 array = people.split( /\s*[,;]\s* # comma or semicolon, optionally surrounded by whitespace | # or \s{2,} # two or more whitespace characters | # or [\r\n]+ # any number of newline characters /x) 
+11
source

Source: https://habr.com/ru/post/1392331/


All Articles