Galet
Galet

Reputation: 6309

How to split string into an array

Here is my sample string with values.

Sample String:-

str = "2014-09-04 21:12:05" "5469687123030383463" "192.168.1.2" "4" "7879" "0" "43" "http://www.google.com" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "404" "123" "F" "21549" "50" "0" "-" "-" "Test1":"T" "Test2":"T" "Test3":"F" "Test4":"T" "Test5":"F" "Test6":"T" "Test7":"F" 

If i split using space:-

Use str.split(' ')

Result is:-

["\"2014-09-04", "21:12:05\"", "\"5469687123030383463\"", "\"192.168.1.2\"", "\"4\"", "\"7879\"", "\"0\"", "\"43\"", "\"http://www.google.com\"", "\"Mozilla/5.0", "(X11;", "Linux", "x86_64)", "AppleWebKit/537.36", "(KHTML,", "like", "Gecko)", "Chrome/35.0.1916.153", "Safari/537.36\"", "\"404\"", "\"123\"", "\"F\"", "\"21549\"", "\"50\"", "\"0\"", "\"-\"", "\"-\"", "\"Test1\":\"T\"", "\"Test2\":\"T\"", "\"Test3\":\"F\"", "\"Test4\":\"T\"", "\"Test5\":\"F\"", "\"Test6\":\"T\"", "\"Test7\":\"F\""] 

But i need like below:-

Expected result:-

["\"2014-09-04 21:12:05\"", "\"5469687123030383463\"", "\"192.168.1.2\"", "\"4\"", "\"7879\"", "\"0\"", "\"43\"", "\"http://www.google.com\"", "\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36\"", "\"404\"", "\"123\"", "\"F\"", "\"21549\"", "\"50\"", "\"0\"", "\"-\"", "\"-\"", "\"Test1\":\"T\"", "\"Test2\":\"T\"", "\"Test3\":\"F\"", "\"Test4\":\"T\"", "\"Test5\":\"F\"", "\"Test6\":\"T\"", "\"Test7\":\"F\""] 

How can i do in ruby to achieve the expected result.

Upvotes: 0

Views: 56

Answers (1)

Stefan
Stefan

Reputation: 114248

Assuming your string is:

str = '"2014-09-04 21:12:05" "5469687123030383463" "192.168.1.2" "4" "7879" "0" "43" "http://www.google.com" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "404" "123" "F" "21549" "50" "0" "-" "-" "Test1":"T" "Test2":"T" "Test3":"F" "Test4":"T" "Test5":"F" "Test6":"T" "Test7":"F"'

You can use a regular expression with a positive lookahead / lookbehind assertion to match a space that is surrounded by quotes (without capturing the quotes):

str.split(/(?<=") (?=")/)
#=> ["\"2014-09-04 21:12:05\"",
#    "\"5469687123030383463\"",
#    "\"192.168.1.2\"",
#    "\"4\"",
#    "\"7879\"",
#    "\"0\"",
#    "\"43\"",
#    "\"http://www.google.com\"",
#    "\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36\"",
#    "\"404\"",
#    "\"123\"",
#    "\"F\"",
#    "\"21549\"",
#    "\"50\"",
#    "\"0\"",
#    "\"-\"",
#    "\"-\"",
#    "\"Test1\":\"T\"",
#    "\"Test2\":\"T\"",
#    "\"Test3\":\"F\"",
#    "\"Test4\":\"T\"",
#    "\"Test5\":\"F\"",
#    "\"Test6\":\"T\"",
#    "\"Test7\":\"F\""]

Upvotes: 2

Related Questions