This is probably the nastiest regex I ever had to use. It's goal was to highlight Extended Letter Sequences in text. The ELS had already been found, this routine went through and turned the letters of the hidden word into uppercase. This is just a 'capitalise every nth letter' challenge, and I cheated by stripping out the spaces first:
#Block is the chunk of text, word is the 'hidden' word and sep is the
+number of characters in the text between each word
my ($block, $sep, $word)=@_;
my @let = split //, $word;
my $rep;
my $i=1;
foreach my $l (@let) {$l = "(".$l.")(.{$sep})";$rep.=
+ '\u$'.$i++.'$'.$i++;};
my $regexp2 = join "", @let;
my $ev = '$block =~ '."s/$regexp2/$rep/i;";
# print "Now scanning block with $regexp2 and replacemen
+t target $rep\n";
return $block, $word if eval $ev;
The output looks like this:
---This is the original text----------------------------------------
m at times; this thread, like that of Ariadne, when once
unraveled will conduct one through a lab
---The ELS 'FNORD' is in capitals---------------------
mattimesthisthreadlikethatoFariadNewhenOnceunRaveleDwillconductonethro
+ughalab
BTW if there's an obviously better way to do this I'd be delighted to hear about it.
____________________
Jeremy
I didn't believe in evil until I dated it.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.