# Regex question

**URL:** <https://rubytalk.org/t/regex-question/41847>\
**Category:** ruby-talk\
**Created:** [1 November 2007 18:31 UTC](https://rubytalk.org/t/regex-question/41847 "2007-11-01T18:31:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Keller](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@Robert\_Keller](https://rubytalk.org/u/Robert_Keller)\
**Post date:** [1 November 2007 18:31 UTC](https://rubytalk.org/t/regex-question/41847/1 "2007-11-01T18:31:03Z")

</div>

I have a string = "this is text that I want to 9 9.90 89 9 ii8 u"

using str = string.gsub(/(?!\d)\b\s\b(?!\d)/,'-')

i get "this-is-text-that I want-to 9 9.90 89 9-ii8-u"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ^

there problem is that I do not want the "-" to show up if the last char is a digit or the first chat is a digit. Got real close, but I think I need lookbehind which is not supported in ruby?

> **···**
>
> ------------------------------  
> Robert Keller  
> [rlkeller@yahoo.com](mailto:rlkeller@yahoo.com)

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [1 November 2007 19:20 UTC](https://rubytalk.org/t/regex-question/41847/2 "2007-11-01T19:20:23Z")

</div>

Robert Keller wrote:

> I have a string = "this is text that I want to 9 9.90 89 9 ii8 u"
> 
> using str = string.gsub(/(?!\d)\b\s\b(?!\d)/,'-')
> 
> i get "this-is-text-that I want-to 9 9.90 89 9-ii8-u"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ^
> 
> there problem is that I do not want the "-" to show up if the last char  
> is a digit or the first chat is a digit. Got real close, but I think I  
> need lookbehind which is not supported in ruby?

Is this what you want:

&nbsp;&nbsp;&nbsp;&nbsp;str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }

-- Jim Weirich

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [1 November 2007 20:10 UTC](https://rubytalk.org/t/regex-question/41847/3 "2007-11-01T20:10:32Z")

</div>

Jim Weirich wrote:

> Is this what you want:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }

Or simply:

new\_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/) {"#$1-#$2"}  
puts new\_str

--output:--  
this-is-text-that I want-to 9 9.90 89 9 ii8 u

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [2 November 2007 01:54 UTC](https://rubytalk.org/t/regex-question/41847/4 "2007-11-02T01:54:06Z")

</div>

7stud -- wrote:

> Jim Weirich wrote:
> 
> > Is this what you want:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }
> 
> Or simply:
> 
> new\_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/) {"#$1-#$2"}  
> puts new\_str
> 
> --output:--  
> this-is-text-that I want-to 9 9.90 89 9 ii8 u

You can also write it this way, which I think is even simpler since that  
block might be a little confusing:

new\_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/, '\1-\2')

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
