# Regex question

**URL:** <https://www.ruby-forum.com/t/regex-question/118966>\
**Category:** Ruby\
**Created:** [November 1, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/regex-question/118966 "2007-11-01T19:39:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 1, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/regex-question/118966/1 "2007-11-01T19:39:57Z")

</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”  
^ ^

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?

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 1, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/regex-question/118966/2 "2007-11-01T20:20:13Z")

</div>

Robert K. 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”  
> ^ ^
> 
> 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:

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

```

– Jim W.

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 2, 2007, 2:54am UTC](https://www.ruby-forum.com/t/regex-question/118966/3 "2007-11-02T02:54:07Z")

</div>

7stud – wrote:

> Jim W. wrote:
> 
> > Is this what you want:
> > 
> > ```
> > 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’)

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [November 1, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/regex-question/118966/4 "2007-11-01T21:10:34Z")

</div>

Jim W. wrote:

> Is this what you want:
> 
> ```
> 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
