Bill Welense (williamw) wrote in s2practice,
Bill Welense
williamw
s2practice

Perl-like substitution in S2? Want to make custom HTML tag

1. I'm trying to write a function that will do a find-and-replace on the entry text. It would search the entry for foo and replace every instance of foo with bar. Is this even possible?

2. If #1 is possible, what is S2's syntax for a "find-and-replace"/regular expression substitution? Also, if there is such a thing, it'd be important what to know to use as a wildcard.

I test this out in Perl, and all I have to do is:

$my_open = '123';
$my_close = '789';
$_ = $e.text;
s/<foo>(.+?)<\/foo>/$my_open\1$my_close/ig;
$e.text = $_;

In Perl, this would take the value of $e.text (which I presume is the variable for the entry text itself) and look for all instances of a <foo> HTML-style tag set. It would then grab all of the text between the <foo> and </foo> tags and surround it with the text in the $my_open and $my_close variables.

So, in my Perl code above, if $e.text had a value of...
the numbers are <foo>456</foo> and <foo>098</foo>

...and I ran it through this function (subroutine in Perl-speak), I get...
the numbers are 123456789 and 123098789


How would I do this with S2?

...help please?!





The example above was simplified from my original intent. If you're curious what I'm actually trying to do here, see this post in my journal:
http://williamw.livejournal.com/239967.html
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 2 comments