Solved

Ruby RegEx search and replace

Posted on 2008-10-10
3
1,590 Views
Last Modified: 2013-11-13
This should be simple:

text = 'adam|paul|mike'

I want to replace that with   ->   <b>adam</b><b>paul</b><b>mike</b>

So each name seperated by a |  OR a name at the end of the string should be enclosed in <b> tags.  Here's what I have so far:

text.gsub(/[a-z]*[|\Z]/, '<b>\0</b>')

But this leaves off the last name.  I'd also like to replace [a-z]*  with just  .*  - (which for some reason is way off)

As a bonus, is there a way to also get the values inside the <b> tags to drop the  |    -  but that can always easily be done in a second step

Thanks!
0
Comment
Question by:ARACK04
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22692963
use split to break the string in an array, change the diffrent items in place using collect and join the remainder array

text = 'adam|paul|mike'
arr = text.split('|')
arr.collect!{|a| '<b>' + a + '</b>' }
puts arr.join

easy, no?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22692981
of course you can do this in one line
puts text.split('|').collect!{|a| '<b>' + a + '</b>' }.join
0
 

Author Closing Comment

by:ARACK04
ID: 31505258
Not sure why I assumed I needed a regex here.  Thanks!.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
no images after rails 3.1 deploy 7 224
What does 'resources :users' do in rails 1 289
Novice needs help - simple string replace 5 217
Iterate over classes 1 187
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question