3
我想将字符串"hello+world-apple+francisco-rome"
拆分为["hello", "+world", "-apple", "+francisco", "-rome"]
。Ruby中的非破坏性拆分
String::split
实际上失去了分裂元素。任何人都可以这样做?
我想将字符串"hello+world-apple+francisco-rome"
拆分为["hello", "+world", "-apple", "+francisco", "-rome"]
。Ruby中的非破坏性拆分
String::split
实际上失去了分裂元素。任何人都可以这样做?
你可以用这个简单的正则表达式做到这一点:
"hello+world-apple+francisco-rome".scan(/[+\-]?\w+/)
美丽!谢谢。 – 2010-07-30 01:26:52
@Julien Genestoux:这里有一个巧妙的小技巧:如果你在字符类中首先放置'-',那么你不需要转义它:'/ [ - +] /'。 (也适用于''',我相信。) – 2010-07-30 07:56:34