SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast selfmindead's Avatar
    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?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    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.

  3. #3
    SitePoint Enthusiast
    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

  4. #4
    SitePoint Zealot
    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
    But that is almost pointless since gsub does a global substitution on the whole string and will replace multiple items matching the regex. So you could do this without the while loop pretty much. But you asked for the while loop so this is how you would do it.

  5. #5
    SitePoint Enthusiast selfmindead's Avatar
    Join Date
    Nov 2004
    Location
    Las Vegas, NV
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ezmobius1
    Code:
     
     while string =~ /regex/
        string.gsub!(/regex/, 'replacement text')
     end
    But that is almost pointless since gsub does a global substitution on the whole string and will replace multiple items matching the regex. So you could do this without the while loop pretty much. But you asked for the while loop so this is how you would do it.
    the point of the while loop is because "replacement text" will change depending on the return value of the regexp... this is my regexp: /\<\%\=(.*?)\%\>/... the code in the middle of the "<%=" and the "%>" is what is used to grab the replacement text from the database....

    So, hopfully that explains why I needed a while loop. Maybe you guys know some alternatives?

  6. #6
    SitePoint Evangelist
    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
    But of course you wouldnt just assign a string but I think you get the idea.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by selfmindead
    the point of the while loop is because "replacement text" will change depending on the return value of the regexp... this is my regexp: /\<\%\=(.*?)\%\>/... the code in the middle of the "<%=" and the "%>" is what is used to grab the replacement text from the database....

    So, hopfully that explains why I needed a while loop. Maybe you guys know some alternatives?
    You can try an iterator instead of loop

    Code:
    puts "<foo> xxx <bar> yyy".gsub(/<(.*?)>/) {
    	"contents of #$1"
    }

  8. #8
    SitePoint Enthusiast selfmindead's Avatar
    Join Date
    Nov 2004
    Location
    Las Vegas, NV
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know enough to get it done now, thanks alot everyone!

    I'll be sure to post the finished code when I'm done, for archiving purposes :P

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Yakima WA.
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by selfmindead
    the point of the while loop is because "replacement text" will change depending on the return value of the regexp... this is my regexp: /\<\%\=(.*?)\%\>/... the code in the middle of the "<%=" and the "%>" is what is used to grab the replacement text from the database....

    So, hopfully that explains why I needed a while loop. Maybe you guys know some alternatives?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •