如何从字符串中提取字符串?

时间:2021-09-26 07:11:29

I have a long string, consisting of multiple sentences, of various length, divided by a "-".

我有一个长字符串,由多个句子组成,长度不同,除以一个“-”。

I want to iterate over the string and extract everything between the -'s, preferably to an array.

我想遍历字符串并提取- s之间的所有东西,最好是一个数组。

From another thread I found something that gets me pretty close, but not all the way:

从另一个线索,我发现了一些让我很接近的东西,但不是所有的方式:

longString.scan( /-([^-]*)-/)

Needless to say, I am new to Ruby, and especially to RegEx.

不用说,我是Ruby的新手,尤其是RegEx。

2 个解决方案

#1


5  

What's wrong with using String#split?

使用字符串#split有什么问题?

longString.split('-')

#2


4  

Why not just use string.split()?

为什么不直接使用string.split()呢?

longString.split('-');

#1


5  

What's wrong with using String#split?

使用字符串#split有什么问题?

longString.split('-')

#2


4  

Why not just use string.split()?

为什么不直接使用string.split()呢?

longString.split('-');