Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要在不删除分隔符的情况下拆分字符串。有没有一种简单且“面向 Ruby”的方法来做到这一点?
例如,给定这样的字符串:
str = "(This is (a test))"
我需要的是这个:
["(", "This", "is", "(", "a", "test", ")", ")"]
我尝试对字符串使用 split 方法,使用括号"("和")"分隔符,但随后我将它们从返回的数组中删除。任何建议都会有所帮助。
"("
")"
也许是这样的:
str.scan(/\(|\)|\w+/)