Split a word in Ruby for counting

When I split the string "hello world / n" with

"hello world \n".scan(/\w+/)

I get ["hello", "world"]

I would like to consider \ n or \ t as a string.

+5
source share
6 answers

Do you need something like this?

"hello world \n".scan(/\w+|\n/)
+5
source

Use not \w+ to count words. It will separate numbers and words using Unicode as follows:

"The floating point number is 13.5812".scan /\w+/
=> ["The", "floating", "point", "number", "is", "13", "5812"]

The same is true for numbers with other type delimiters "12,000".

In Ruby 1.8, the expression w+worked with Unicode, this has changed. If your string contains Unicode characters, the word will also be split.

"Die Apfelbäume".scan /\w+/
=> ["Die", "Apfelb", "ume"]

There are two options.

  • . ,

    /\p{Letter}+/
    
  • , .

    /\S+/
    

    \S+ /[^ \t\r\n\f]/. , . , , .. .

    . .

+4

\n : , . : \\n.

,

"Hello world \n".split(/ /)
+3
"hello world \n".scan /[\w\n\t]+/
+2

, (, 90- ..)

"hello world \n".split(/[^\w']+/)
+1
source

You can use the named character class [: cntrl:].

irb(main):001:0> "hello world \n".scan(/\w+|[[:cntrl:]]/)
=> ["hello", "world", "\n"]
0
source

All Articles