note
stefan k
Howdy,<br>
just going through some code of mine...
<ul>
<li>
Say you got some variables stored in a hash: name=>value and whant to replace them in a text where they are used in a make-like form: $(VARIABLE)
<code>
foreach (keys %global_variables) {
$content =~ s/\$\($_\)/$global_variables{$_}/g;
}
</code>
<li>When you want to indent a part of text and calculated the indent already you might use:
<code>
$content =~ s/\n/\n$indent /g;
</code>
<li>
Some really often used substitutions are the whitespace deletions: 1. All whitespaces: <code>$line =~ s/\s+//g;</code> 2. Only leading whitespaces: <code>$line =~ s/^\s*//;</code> 3. Only trailing whitespaces: <code>$line =~ s/\s*$//;</code>
<li>Say you got a GUI (in my case it was Gtk) and want to display some messages. In the preview window though only the first line shall be displayed, marking that more is available:
<code>
$text =~ s/\n.*/ [more\.\.\.]/s;
</code>
</ul>
That's it for the moment. The rest of my regexp-substitutions is mostly boring ;-)
<p>
Update: Added the missing parens around VARIABLE
<p>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><small>Regards...</small></td>
<td align="right"><small>
<a href="http://www.skamphausen.de">Stefan</a>
</small></td>
</tr>
<tr>
<td colspan="2" align="center">
<small>you begin bashing the string with a +42 regexp of confusion</small>
</td>
</tr>
</table>
125523
125523