08/08/11
Paul Savage
tags:  

Using RegEx to prefix or postfix


While it’s programmatically probably not the best way to do this type of operation, you may find that you need to use a Regular expression to prepend or postpend a string. I’ve used standard regular expression notation here where you cite the replacements in [].

Prefix String with RegEx

Search for [(^)] Replace with [Pre: $1] , this will add Pre: to the start all your stings.
E.g.

  • String : Blackdog.ie raises first round of VC
  • Search : (^)
  • Replacement : CNN : $1
  • Result : CNN : Blackdog.ie raises first round of VC

Postfix a string with RegEx

Search for [($)] Replace with [$1 : Post], this will add : Post to the end of all your strings

  • String : Blackdog.ie raises first round of VC
  • Search : ($)
  • Replacement : $1 : CNN
  • Result : Blackdog.ie raises first round of VC : CNN

I use this for http://pipes.yahoo.com/ which is a handy tool for mashing RSS feeds together. I did want to attribute where the feeds were coming from, and putting the source in the title really improve the feed.

Leave a Reply