2015-03-16 2 views
0

Я пытаюсь сделать это. Например:Разделив предложение на слова, используя regexp

"This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all." 

следует разделить на отдельные слова:

This 
is 
a 
sentence 
I'm 

и так далее.

Я просто пытаюсь написать регулярное выражение. Я знаю, что это было бы довольно легко с использованием разделителя или двух, но пытаться узнать больше о regexp.

+0

просто разделить в соответствии с этим '\ S +' регулярное выражение. или используйте 'string.split()' –

+1

Напишите «так далее». Ваш вопрос неоднозначен. Мы не можем сказать, что вы хотите после этого. И, каков ваш вопрос? – sawa

+1

Вы хотите «писать» или «писать», 'in:' или 'in' ...? – Toto

ответ

3

Просто разделите свой вход в соответствии с одним или несколькими символами пробела.

> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".split(/\s+/) 
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."] 

> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".split() 
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."] 

ИЛИ

Матч один или более не пробельные символы.

> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all.".scan(/\S+/) 
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."] 
+0

работал сразу. Спасибо друг! –

0

использование split

2.0.0-p481 :001 > a="This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all." 
=> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all." 
2.0.0-p481 :002 > a.split 
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."] 
2.0.0-p481 :003 > 

ИЛИ USE LOOP к кадру A слово в каждой строке

2.0.0-p481 :036 > a="This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all." 
=> "This is a sentence I'm currently writing, potentially with punctuation dotted in: item1, item2, item3. That is all." 
2.0.0-p481 :037 > a.split.each{ |i| puts "#{i}"} 
This 
is 
a 
sentence 
I'm 
currently 
writing, 
potentially 
with 
punctuation 
dotted 
in: 
item1, 
item2, 
item3. 
That 
is 
all. 
=> ["This", "is", "a", "sentence", "I'm", "currently", "writing,", "potentially", "with", "punctuation", "dotted", "in:", "item1,", "item2,", "item3.", "That", "is", "all."] 
2.0.0-p481 :038 > 
Смежные вопросы