Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Using parse to make intelligent substitutions.

From: mat:plothatching at: 28-Apr-2003 16:27

Hello, Interesting thing I had to do today. Take a block of test and if there's any occurrence of "blog" followed by a number, then this into a URL. I thought it about time that I actually got off my rear and worked out Parse to a little more advanced level than my usual going around the houses. Here's my stab; <--cut--> InputText: "This is blog 42 and then some stuff followed by blog 54 right?" blogsfound: copy [] parse test [any [thru "blog " copy blognumstring to " " (if not error? try [blognum: to-integer blognumstring] [ append blogsfound blognum ]) ] ] foreach foundblog blogsfound [ replace InputText rejoin["blog "foundblog] rejoin[{<A HREF="http://www.blah.com/blog.php?blog=}foundblog{">blog }foundblog{</A>}] ]
>> print InputText
This is <A HREF="http://www.blah.com/blog.php?blog=42">blog 42</A> and then some stuff followed by <A HREF="http: //www.blah.com/blog.php?blog=54">blog 54</A> right? <--cut--> It works but it's ugly. Another way, I guess, would be to build a complete new string with the substitutions from within the parse statement. However I'd love to see someone's proper elegant Rebolesque way of doing it that will end up with me slapping my forehead going "Duh!" again. After gazing at lots of Parse documents I couldn't work out how I could use 'some digits' type stuff to parse on and then end up with the digits being passed to the rebol expression doing all the replacing and stuff. Thanks guys. I love Rebol, I just wish I was better at it :) Regards, Mat Bettinson - +44-(0)20-83401514.