Is there an easier way to get a match before a position in a string?

M

Michael W. Ryder

I am trying to find an easier way to split a string into printable
lines. I could use the method of checking for a space using a loop and
going back from the desired length or I could use the following:

def strbrk(str, len)
if str.length <= len
return str.length
end
tmp = str[0...len].reverse
x = tmp =~ / /
if x == nil
return -1
else
return len - x
end
end

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.
 
G

George

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.

str.rindex(/ /, len) ?
 
Y

yermej

I am trying to find an easier way to split a string into printable
lines. I could use the method of checking for a space using a loop and
going back from the desired length or I could use the following:

def strbrk(str, len)
if str.length <= len
return str.length
end
tmp = str[0...len].reverse
x = tmp =~ / /
if x == nil
return -1
else
return len - x
end
end

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.

This should work:

/ [^ ]*$/

It looks for one space, followed by 0 or more occurrences of anything
besides a space, followed by the end of the string.
 
W

William James

I am trying to find an easier way to split a string into printable
lines. I could use the method of checking for a space using a loop and
going back from the desired length or I could use the following:

def strbrk(str, len)
if str.length <= len
return str.length
end
tmp = str[0...len].reverse
x = tmp =~ / /
if x == nil
return -1
else
return len - x
end
end

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.

def str_break( str, len )
str.size > len and str[0..len].rindex(' ') or str.size
end



str = "\
I don't necessarily need code examples -- but if anyone has
ideas for a best approach to specifying a line wrap width
(breaking between words for lines no longer than a specific
column width) for output from a Ruby script, I'd love to
hear about it."

X = 40
puts str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)
 
M

Michael W. Ryder

William said:
I am trying to find an easier way to split a string into printable
lines. I could use the method of checking for a space using a loop and
going back from the desired length or I could use the following:

def strbrk(str, len)
if str.length <= len
return str.length
end
tmp = str[0...len].reverse
x = tmp =~ / /
if x == nil
return -1
else
return len - x
end
end

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.

def str_break( str, len )
str.size > len and str[0..len].rindex(' ') or str.size
end
This is much better than my solution, I missed rindex somehow.
 
S

Siep Korteling

William James wrote:
...
str = "\
I don't necessarily need code examples -- but if anyone has
ideas for a best approach to specifying a line wrap width
(breaking between words for lines no longer than a specific
column width) for output from a Ruby script, I'd love to
hear about it."

X = 40
puts str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)

Kaspar Schiess wrote text-reform (gem install text-reform)

require 'text/reform'

r = Text::Reform.new
while line=gets
puts r.format('['*40, line)
end


Here he says it does hyphenate:
http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
 
M

Michael W. Ryder

William said:
I am trying to find an easier way to split a string into printable
lines. I could use the method of checking for a space using a loop and
going back from the desired length or I could use the following:

def strbrk(str, len)
if str.length <= len
return str.length
end
tmp = str[0...len].reverse
x = tmp =~ / /
if x == nil
return -1
else
return len - x
end
end

I could not find any way to get a regexp to search from the end of the
string which would have eliminated the need to reverse the string.

def str_break( str, len )
str.size > len and str[0..len].rindex(' ') or str.size
end

I did find one more "improvement" on your code after reading further on
rindex.

def str_break( str, len )
str.size > len and str.rindex(' ', len) or str.size
end

It appears to work the same and give the same results but I am not sure
if there was a reason for the way you did it that I missed.
Thanks for the push in the right direction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top