regex question

R

rlkeller

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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

jim

Robert said:
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 Weirich
 
B

bbxx789_05ss

Jim said:
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
 
B

bbxx789_05ss

7stud said:
Jim said:
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')
 

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,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top