0

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.

2
  • 2
    You cannot have such string. Commented Sep 8, 2014 at 7:41
  • How do you do it, as a human? Write down the rules, then transform them to code. Commented Sep 8, 2014 at 8:01

1 Answer 1

2

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\""]
Sign up to request clarification or add additional context in comments.

4 Comments

owww I didnt notice that the strings were in double quotes ... lol
@stefan if my string like this , then how i split into array of values.str = '"2014-09-04 21:12:05" 5469687123030383463 192.168.1.2 4 7879 0 43 "www.test.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
@karan that's a different task, please ask a separate question.
@Stefan I created ticket here stackoverflow.com/questions/25720969/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.