SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Dec 22, 2005, 12:27 #1
- Join Date
- Nov 2004
- Location
- Las Vegas, NV
- Posts
- 28
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Using regular expressions to replace...
I would greatly appreciate it if somoene could help me out with something... I'm having trouble writing a loop in RoR that continues replacing based on a regular expression until there are no more matches left for the regular expression.
The part that confuses me the most is how to make a while loop continue until a regular expression is not returning values anymore... can anyone help me out with this?
-
Dec 22, 2005, 13:51 #2
- Join Date
- Jun 2003
- Location
- Iowa, USA
- Posts
- 3,749
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Doesn't gsub handle this?
Code:$ irb >> str = "this is a target string with some targets in it" => "this is a target string with\nsome targets in it" >> str.gsub(/target/, 'bullseye') => "this is a bullseye string with\nsome bullseyes in it"
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.
-
Dec 22, 2005, 18:29 #3
- Join Date
- Jun 2004
- Location
- Stillwater, MN
- Posts
- 96
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can use a string for the search argument if a regex is not necessary.
Rad Smith
My blog
-
Dec 22, 2005, 22:45 #4
- Join Date
- Nov 2004
- Location
- Yakima WA.
- Posts
- 100
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:while string =~ /regex/ string.gsub!(/regex/, 'replacement text') end
-
Dec 22, 2005, 23:38 #5
- Join Date
- Nov 2004
- Location
- Las Vegas, NV
- Posts
- 28
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by ezmobius1
So, hopfully that explains why I needed a while loop. Maybe you guys know some alternatives?
-
Dec 23, 2005, 01:07 #6
- Join Date
- Jun 2004
- Location
- California
- Posts
- 440
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You could probably use the while loop and directly modify the "string" variable and so it'd slowly run out of matches:
Code:while string =~ /regex/ string = 'blablabla' end
-
Dec 23, 2005, 03:28 #7
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by selfmindead
Code:puts "<foo> xxx <bar> yyy".gsub(/<(.*?)>/) { "contents of #$1" }
-
Dec 23, 2005, 10:35 #8
-
Dec 23, 2005, 16:38 #9
- Join Date
- Nov 2004
- Location
- Yakima WA.
- Posts
- 100
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by selfmindead
That regex looks suspiciously like erb tag syntax. Is there any reason you are parsing that by hand instead of using the built in erb tag parser in the stdlib? It could save you some hassle.
-Ezra
Bookmarks