andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-05 21:52 UTC
gsub question, not a regex question...including part of the original in the sub...
Hi all, I''ve got a simple bulletin board type of code in part of my application, and I want to implement phpbb-like tags for users to add to posts. I strip HTML out of the posts, of course. I can handle things like [b] => <b> with a simple gsub, but there''s one that is throwing me for a loop. I want users to be able to quote other users, and have the quoted text show up with an "originally posted by username" and some CSS code. I can gsub the [/QUOTE] into a </div> but how do I change [QUOTE=username] into: <div class="quote"><p><em>Originally posted by username</em></p> Yes, I can do most of it with a gsub, but how do I pull "username" out of the original text and put it where it belongs in the substituted text? Or is there a better way to do this all together? Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Wang
2007-Sep-06 04:21 UTC
Re: gsub question, not a regex question...including part of the original in the sub...
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi all, > > I''ve got a simple bulletin board type of code in part of my > application, and I want to implement phpbb-like tags for users to add > to posts. I strip HTML out of the posts, of course. I can handle > things like [b] => <b> with a simple gsub, but there''s one that is > throwing me for a loop. I want users to be able to quote other users, > and have the quoted text show up with an "originally posted by > username" and some CSS code. I can gsub the [/QUOTE] into a </div> > but how do I change [QUOTE=username] into: > > <div class="quote"><p><em>Originally posted by username</em></p> > > Yes, I can do most of it with a gsub, but how do I pull "username" out > of the original text and put it where it belongs in the substituted > text?Shortened to not wrap: "[q=u]".gsub(/\[q=(\w+)\]/, ''<div class="q">blah \1</div>'') => "<div class=\"q\">blah u</div>" ( ) are used for grouping in regexes and \1, \2, etc. are used to reference the groups in the replacement pattern. So (\w+) creates a group of all the "word" characters after the q= and before the ] and puts that into \1. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Reasonably Related Threads
- readLines interaction with gsub different in R-dev
- regex - optional part isn't considered in replacement with gsub
- readLines interaction with gsub different in R-dev
- regex - optional part isn't considered in replacement with gsub
- R 2.10.0: Error in gsub/calloc