Thread RegEx-Frage (11 answers)
Opened by Gast at 2003-09-30 17:56

Strat
 2003-09-30 18:13
#59083 #59083
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
[quote=kmonster,30.09.2003, 16:07]also ich würde jetzt eins nach dem anderen suchen und ersetzten. zuerst würde dann das innere gefunden dann das drum rum. gibt aber bestimmt eine elegantere lösung.[/quote]
vielleicht so?
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#! /usr/bin/perl
use warnings;
use strict;
use CGI ();
use HTML::Entities();
use Regexp::Common qw(balanced);
use URI::Find::Schemeless ();

$| = 1;    # no suffering from buffering

use vars qw(%TagsBlocked %TagsFastReplace %TagsLastReplace $HighlightBin);

# use external program from
# http://gnu.j1b.org/software/src-highlite/source-highlight.html
$HighlightBin = 'E:\apps\gnu\src-highlight\bin\source-highlight.exe';

# tags within which no further replacements should be done

# better replacing the font stuff by CSS
%TagsBlocked = (
   perle => {
       -start => qq~<br><b>PERL:</b>
<table border="0" width="95%" cellpadding="8"><tr><td bgcolor="dddddd">
<font face="Courier New, Courier, mono">~,
       -end    => qq~</font></td></tr></table>~,
       -string => \&HighlightPerlCode,
   },
   code => {
       -start => qq~<br><b>CODE:</b>
<table border="0" width="95%" cellpadding="4"><tr><td bgcolor="dddddd">
<font face="Courier New, Courier, mono">~,
       -end    => qq~</font></td></tr></table>~,
       -string => sub { return $_[0] },
   },
   perldoc => {
       -start  => "",
       -end    => "",
       -string => sub {
           return qq~ <a href="http://url/$_[0]">perldoc $_[0]</a> ~;
       },
   },
);

# tags to be replaced on the fly
%TagsFastReplace = (
   b => {
       -start  => "<b>",
       -end    => "</b>",
       -string => sub { return $_[0] },
   },
   i => {
       -start  => "<i>",
       -end    => "</i>",
       -string => sub { return $_[0] },
   },
);

# tags that may be recursive or containing other tags
%TagsLastReplace = (
   quote => {
       -start =>
         qq~<div align="center"><table border="1" width="95%"><tr><td>~,
       -end    => qq~</td></tr></table></div>~,
       -string => sub { return $_[0] },
   },
);

{
   local $/;    # slurp in data
   my $posting = <DATA>;

   &ParseText($posting);
}

# ------------------------------------------------------------
sub ParseText {
   my ($text) = @_;

   $text = &CGI::escapeHTML($text);
   $text =~ s/\n/<br>\n/g;    # replace newlines by <br>

   my @scanned = ();

   # extract and replace %TagsBlocked
   foreach my $tag ( keys %TagsBlocked ) {

       # @scanned yet empty, start with $text
       unless ( scalar(@scanned) ) {
           @scanned = &PostFilterBlocked( $text, $tag );
       }                      # unless

       # @scanned filled, start scanning the rest
       else {
           my @scanned2 = ();    # temporary variable
           foreach (@scanned) {

               # skip already scanned parts
               push ( @scanned2, $_ ), next if ref($_);

               # scan only if scalar
               push ( @scanned2, &PostFilterBlocked( $_, $tag ) );

           }                     # foreach
           @scanned = @scanned2;
       }    # else
   }    # foreach

   # replace %TagsFastReplace
   foreach my $tag ( keys %TagsFastReplace ) {

       # @scanned yet empty, start with $text
       unless ( scalar(@scanned) ) {
           @scanned = &PostFilterFastReplace( $text, $tag );
       }    # unless

       # @scanned filled, start scanning the rest
       else {
           my @scanned2 = ();    # temporary variable
           foreach (@scanned) {

               # skip already scanned parts
               push ( @scanned2, $_ ), next if ref($_);

               # scan only if scalar
               push ( @scanned2, &PostFilterFastReplace( $_, $tag ) );

           }                     # foreach
           @scanned = @scanned2;
       }    # else
   }    # foreach

   # prepare finder sub for replacing URIs in Text with HTML-Links
   my $uriFinder = URI::Find::Schemeless->new(
       sub {
           return qq~<a href=\"~
             . &HTML::Entities::encode_entities("$_[0]")
             . qq~\" target="_blank">~
             . &HTML::Entities::encode_entities( $_[1] ) . '</a>';
       }
   );

   # replace URIs in Text with HTML-Links
   ref($_) or $uriFinder->find( \$_ ) foreach @scanned;

   # build string and replace code/perl/...-tags with [\0\0$tag]
   # to prevent further parsing
   my $string = join (
       "",
       map {
           ref($_)
             ? map {
               my $r = $_;
               $r =~ s/(\[)(.+\])/$1."\0\0".$2/gse;
               $r;
             } values( %{$_} )
             : $_;
         } @scanned
   );

   # replace stuff that might contain other tags (like quote)
   foreach my $tag ( keys %TagsLastReplace ) {
       $string = &PostFilterLastReplace( $string, $tag );
   }    # foreach

   $string =~ s/\0\0//g;    # remove \0\0 (from [\0\0$tag])
   print "\n\n", $string;

}    # ParseText

# ------------------------------------------------------------
sub PostFilterBlocked {
   my ( $text, $tag ) = @_;

   my $startLength = length("[$tag]");
   my $endLength   = length("[/$tag]");

   my @scanned = ();

   my $startPos = index( $text, "[$tag]" );
   my $lastPos  = 0;

   print "\nScanning for tag [$tag]";
   while ( $startPos != -1 ) {
       print " $startPos";

       # add leading part to @scanned
       my $string = substr( $text, $lastPos, $startPos - $lastPos );
       push ( @scanned, $string ) if length $string;

       # check if [$tag]...[/$tag]-block found
       $lastPos = index( $text, "[/$tag]", $startPos + $startLength );
       print "-$lastPos";
       unless ( $lastPos == -1 ) {    # if found

           # push reference to @scanned: { $tag => $string }
           my $string = substr(
               $text,
               $startPos + $startLength,
               $lastPos - $startPos - $startLength
           );
           $string =~ s/^\r?\n//g;
           $string =~ s/\r?\n$//g;

           # replace tags with replacement
           $string =
             $TagsBlocked{ lc($tag) }->{ -start }
             . $TagsBlocked{ lc($tag) }->{ -string }->($string)
             . $TagsBlocked{ lc($tag) }->{ -end };

           # add to queue as reference
           push ( @scanned, { $tag => $string } ) unless $string =~ /^\s*$/;

       }    # if

       else {    # if not found

           # push rest of $text to @scanned (as string)
           my $string = substr( $text, $startPos, length($text) - $startPos );

           # if last line was in @TagsBlocked, add new array element
           if ( ref $scanned[-1] ) {
               push ( @scanned, $string ) if length $string;
           }

           # if not, append as text to the last element
           else {
               $scanned[-1] .= $string;
           }    # else

           last;    # and exit while

       }    # else

       # (re-)initialize next search position
       $lastPos += $endLength;

       # search for next [tag]
       $startPos = index( $text, "[$tag]", $lastPos );

   }    # while

   # care for rest of $text
   if ( $lastPos != -1 ) {
       my $string = substr( $text, $lastPos, length($text) - $lastPos );
       push ( @scanned, $string ) if length($string);
   }    # if

   return @scanned;
}    # PostFilterBlocked

# ------------------------------------------------------------
sub PostFilterFastReplace {
   my ( $text, $tag ) = @_;
   print "\nScanning for tag [$tag]";

   my $startLength = length("[$tag]");
   my $endLength   = length("[/$tag]");

   my @scanned = ();

   while (
       $text =~ s/
   \[\Q$tag\E\]
   (.+?)
   \[\/\Q$tag\E\]
   /
   $TagsFastReplace{lc($tag)}->{-start} .
   $TagsFastReplace{lc($tag)}->{-string}->($1) .
   $TagsFastReplace{lc($tag)}->{-end}
   /xseig
     )
   {
       1;
   }    # while

   return $text;
}    # PostFilterFastReplace

# ------------------------------------------------------------
sub PostFilterLastReplace {
   my ( $string, $tag ) = @_;
   print "\nScanning for tag [$tag]";

   my $startLength = length("[$tag]");
   my $endLength   = length("[/$tag]");

   1 while $string =~ s!
$RE{balanced}{-begin => "[$tag]"}{-end => "[/$tag]"}{-keep}
   !
$TagsLastReplace{lc($tag)}->{-start} .
$TagsLastReplace{lc($tag)}->{-string}->
(substr ($1, $startLength, -$endLength) ) .
$TagsLastReplace{lc($tag)}->{-end};
   !gex;

   return ($string);
}    # PostFilterLastReplace

# ------------------------------------------------------------
sub HighlightPerlCode {
   my $code = shift;

   use Perl::Tidy;
   my @dest;
   perltidy(
       source      => \$code,
       destination => \@dest,

       #      argv => '-html',
   );

   return join ( "", @dest );

}    # HighlightPerlCode

# ------------------------------------------------------------

Hallo Leute,

[b]das[/b] ist [i]mein [b]erstes[/i] Posting:
[perldoc]CGI[/perldoc] oder so.
[perle]#! /usr/bin/perl
use warnings;
use strict;
use CGI ();
my $cgi = CGI->new();[/perle]
und das[/b] ist was anderes:
[perldoc]CGI[/perldoc]
[perle]#! /usr/bin/perl
use warnings;
use strict;
use CGI ();
my $cgi = CGI->new();
my $string = "[perldoc]CGI[/perldoc]";[/perle]
normaltext
[quote]error
error
[quote]Strat, 12.08.2003 14:04
[URL=ftp://ftp.gnu.org/]ftp://ftp.gnu.org/[/URL] ftp.gnu.org
Quoting Level 1
[quote]
Quoting Level 2
[perle]#! /usr/bin/perl
@list = (1..30);
foreach (0..$#list) {
    print "$list[$_]";
}[/perle]
[URL=http://www.fabiani.net/]http://www.fabiani.net/[/URL]
[/quote]
Quoting Level 1 www.fabiani.net
[/quote]
\n\n

<!--EDIT|Strat|1064931459-->
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/

View full thread RegEx-Frage