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

**URL:** <https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738>\
**Category:** Ruby\
**Created:** [January 21, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738 "2008-01-21T20:15:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/1 "2008-01-21T20:15:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/2 "2008-01-21T21:43:08Z")

</div>

On Jan 22, 2008 5:14 AM, Michael W. Ryder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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) ?

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/3 "2008-01-21T21:50:53Z")

</div>

On Jan 21, 1:12 pm, “Michael W. Ryder” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/4 "2008-01-21T22:25:45Z")

</div>

On Jan 21, 1:12 pm, “Michael W. Ryder” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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+/)

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/5 "2008-01-21T23:35:16Z")

</div>

William J. 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 S. 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/](http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/)

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/6 "2008-01-21T23:40:49Z")

</div>

William J. wrote:

> > tmp = str[0…len].reverse
> 
> 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.

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [January 21, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/is-there-an-easier-way-to-get-a-match-before-a-position-in-a-string/126738/7 "2008-01-21T23:01:05Z")

</div>

William J. wrote:

> > tmp = str[0…len].reverse
> 
> 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.
