Thread Regex-Problem (3 answers)
Opened by perlcharly at 2007-07-25 13:56

renee
 2007-07-25 14:11
#78830 #78830
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/perl

use strict;
use warnings;

my $string = "Bei der folgenden Razzia im Teamhotel des Rennstalls [Astana] spielten sich skurrile Szenen ab. Immer mehr Gendarmen, darunter auch PolizeiƤrzte, trafen bei [Astana] ein.";

my $zeichen = '[';

(my $short = $string) =~ s/^.*?(\w+\s\Q$zeichen\E.*)$/$1/;
print $short;


Fuer das explain habe ich \Q$zeichen\E mal durch \[ ersetzt...

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
The regular expression:
(?-imsx:^.*?(\w+\s\[.*)$)


matches as follows:

NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
\[ '['
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\n\n

<!--EDIT|renee|1185358511-->
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread Regex-Problem