Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex question

Reply
Thread Tools

regex question

 
 
rlkeller@yahoo.com
Guest
Posts: n/a
 
      11-01-2007
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?

------------------------------
Robert Keller
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
jim@weirichhouse.org
Guest
Posts: n/a
 
      11-01-2007
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"
> ^ ^
>
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
bbxx789_05ss@yahoo.com
Guest
Posts: n/a
 
      11-01-2007
Jim Weirich 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
bbxx789_05ss@yahoo.com
Guest
Posts: n/a
 
      11-02-2007
7stud -- wrote:
> Jim Weirich 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')


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments