# The easiest way to separate substrings from a line string

**URL:** <https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795>\
**Category:** Ruby\
**Created:** [July 9, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795 "2009-07-09T12:12:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/1 "2009-07-09T12:12:01Z")

</div>

I have working on tag system and There is information like this

“important urgent project 2009”

i want to seperate into word, one word at the time is okay

What is the easiest way to separate those string word by word, space  
between each word might has more than 1.

My idea is to use loop to check character by character that it is space  
or not, and then cut the part, but i thought it might have easier way  
that i don’t know

Thank you in advance

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/2 "2009-07-09T12:17:12Z")

</div>

Hi –

On Thu, 9 Jul 2009, Sarawut Poaitwinyu wrote:

> or not, and then cut the part, but i thought it might have easier way  
> that i don’t know

words = string.split

When you call split with no argument, it splits on whitespace  
(including more than one character).

David

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 12:21pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/3 "2009-07-09T12:21:54Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Thu, 9 Jul 2009, Sarawut Poaitwinyu wrote:
> 
> > or not, and then cut the part, but i thought it might have easier way  
> > that i don’t know
> 
> words = string.split
> 
> When you call split with no argument, it splits on whitespace  
> (including more than one character).
> 
> David

```
   Thank you , i will try
```

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/4 "2009-07-09T18:07:30Z")

</div>

2009/7/9 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > What is the easiest way to separate those string word by word, space  
> > between each word might has more than 1.
> > 
> > My idea is to use loop to check character by character that it is space  
> > or not, and then cut the part, but i thought it might have easier way  
> > that i don’t know
> 
> words = string.split
> 
> When you call split with no argument, it splits on whitespace  
> (including more than one character).

I am more like the “positive” guy - meaning explicitly defining what I  
want returned. I would do

words = string.scan /\w+/

That way dot, question mark and other signs won’t hurt. It may not  
make a difference but it’s probably good to see different approaches.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/5 "2009-07-09T18:52:01Z")

</div>

Hi –

On Fri, 10 Jul 2009, Robert K. wrote:

> > > 
> > 
> > (including more than one character).
> 
> I am more like the “positive” guy - meaning explicitly defining what I  
> want returned. I would do
> 
> words = string.scan /\w+/
> 
> That way dot, question mark and other signs won’t hurt. It may not  
> make a difference but it’s probably good to see different approaches.

string.split does explicitly define what I want back; it’s just  
something different from what you want back 🙂 It depends exactly how  
you define “word”. I was assuming it was /\S+/ but it may indeed be  
/\w+/ (or maybe /[^\W\d\_]+/ or something).

David

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 9, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/6 "2009-07-09T22:41:42Z")

</div>

2009/7/9 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Fri, 10 Jul 2009, Robert K. wrote:
> 
> > 2009/7/9 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> > 
> > > On Thu, 9 Jul 2009, Sarawut Poaitwinyu wrote:

> > That way dot, question mark and other signs won’t hurt. It may not  
> > make a difference but it’s probably good to see different approaches.
> 
> string.split does explicitly define what I want back; it’s just  
> something different from what you want back 🙂

That’s true. I just wanted to make the point that there are these two  
major approaches: define positively what you want in your result or  
define it ex negativo, i.e. state what you want to use as separator.

The whole point is that both approaches may behave identical with the  
original set of test data but will exhibit different behavior as soon  
as the input changes. If you use #split, you might get something you  
did not want in the first place. With #scan you won’t notice - which  
could be bad as well.

The super safe variant would be to first do a match on the whole  
string to ensure it does contain expected data only and fail if not.  
After that it does not matter any more what extraction method one  
uses.

> It depends exactly how  
> you define “word”. I was assuming it was /\S+/ but it may indeed be  
> /\w+/ (or maybe /[^\W\d\_]+/ or something).

Absolutely.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 13, 2009, 3:47am UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/7 "2009-07-13T03:47:23Z")

</div>

Thank you for everyone again, it seems that you guys discussed sort of  
regular expression that i didn’t understand but thank you for it anyone,  
i will try to research about it later

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 13, 2009, 11:48am UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/8 "2009-07-13T11:48:51Z")

</div>

Hi –

On Mon, 13 Jul 2009, Dave B. wrote:

> Moving off-topic from the thread, but David Black wrote:
> 
> > … /\w+/ (or maybe /[^\W\d\_]+/ or something).
> 
> Do people actually say /[^\W\d\_]/ instead of /[a-z]/i? The latter is  
> much easier for me to read. Does the former include non-latin  
> word-characters?

Yes:

> > s = “\u00e9”  
> > =\> “Ã©”
> 
> > /\w/.match(s)  
> > =\> #\<MatchData “Ã©”\>
> 
> > /[a-z]/.match(s)  
> > =\> nil

Another choice would be:

/[[:alpha:]]/

which I believe would do the same thing that my character class did  
(unless there are some exclusions, inclusions, or edge cases I’m not  
remembering).

David

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 13, 2009, 5:11am UTC](https://www.ruby-forum.com/t/the-easiest-way-to-separate-substrings-from-a-line-string/171795/9 "2009-07-13T05:11:38Z")

</div>

Moving off-topic from the thread, but David Black wrote:

> … /\w+/ (or maybe /[^\W\d\_]+/ or something).

Do people actually say /[^\W\d\_]/ instead of /[a-z]/i? The latter is  
much easier for me to read. Does the former include non-latin  
word-characters?

Cheers,  
Dave B.
