Find single-word words with spaces

There is a suggestion:

Ala ma kota i psa.

      

We can see that the whole sentence is on one line, but if we put it in a small div, it changes.

In Polish we have single letter words like "i" (and) or "w" (in, with), etc.

These words / letters don't look good when left at the end of the line, so we place them on the next one:

Ala ma kota 
i psa.

      

instead:

Ala ma kota i
psa.

      

Problem:

I am trying to find all the individual letters, so I can replace the final space with a noble space. This should do the trick.

+3


source to share


3 answers


From the discussion, it seems that the lines have not yet been separated and you only want to catch the one letter words "i", "o" or "w" and you want to allow the comma after the word. Let's allow semicolons, just to make it more interesting.

You can do something like this:

def no_orphan( str )
    str.gsub( /( [iow][,;]?) /, '\1 ' )
end

      



Example in irb:

irb(main):001:0> def no_orphan( str )
irb(main):002:1>     str.gsub( /( [iow][,;]?) /, '\1 ' )
irb(main):003:1> end
=> nil
irb(main):004:0> no_orphan 'Ala ma kota i psa.'
=> "Ala ma kota i psa."
irb(main):005:0> no_orphan 'Ala ma kota i, psa.'
=> "Ala ma kota i, psa."
irb(main):006:0> no_orphan 'Ala ma kota i; psa.'
=> "Ala ma kota i; psa."
irb(main):007:0> no_orphan 'Test Rocky V with space.'
=> "Test Rocky V with space."
irb(main):008:0> 

      

+2


source


You can use the following to match all single words at the end:

\b[a-zA-Z]$     //or \b\p{L}$

      

And then you can apply your trick.

See DEMO



Change: . If you want to match the trailing space with a space, use the following:

\s+[a-zA-Z]\b[^a-zA-Z]*

      

See DEMO

+2


source


\s(\w[,;]?) *\n

will match one letter at the end of the line. You can replace this with"\n"+group(1)

0


source







All Articles