Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split by two different delimiters

Tags:

string

split

ruby

I have strings like this 'some-dasd\dasd-dasdas\dasdas-dasd-das\dsad'. I need to split the string to array by two different symbols '\' and '-', so I would like to get the array ['some', 'dasd', 'dasd', 'dasdas', 'dasdas', 'dasd', 'das' ,'dsad'].

What is the best way to do it?

like image 770
ceth Avatar asked Feb 21 '12 10:02

ceth


People also ask

How do you split a string by two delimiters?

To split a string with multiple delimiters: Use the str. replace() method to replace the first delimiter with the second. Use the str. split() method to split the string by the second delimiter.

Can split () take two arguments?

split() method accepts two arguments. The first optional argument is separator , which specifies what kind of separator to use for splitting the string. If this argument is not provided, the default value is any whitespace, meaning the string will split whenever .

How do I split a string with multiple delimiters in SQL?

Using the STUFF & FOR XML PATH function we can derive the input string lists into an XML format based on delimiter lists. And finally we can load the data into the temp table..

Can we pass multiple separators in split () Python?

Split the string with multiple separatorsWe can also specify multiple characters as separators. For this, we need to make use of the re module of Python and import the re. split() function.


1 Answers

"ome-dasd\dasd-dasdas\dasdas-dasd-das\dsad".split(/\\|-/)

should do the trick.

like image 133
lucapette Avatar answered Oct 04 '22 04:10

lucapette