Leser: 22
1 2 3
my $datei = 'Mein Name ist $hans§.'; my @platzhalter = $datei =~ /\$(.+?)[^A-Za-z0-9]/g; print join ', ', @platzhalter;
my @platzhalter = grep {length} $datei =~ /\$(.*?)[^A-Za-z0-9]/g;
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/perl use strict; use warnings; my %ersetzungen = ( hans => 'Hallo', test => 'Welt', ); my $datei = 'Ein Test: $hans $test.'; my @platzhalter = $datei =~ s/(?<=\$)(.+?)(?![A-Za-z0-9])/$ersetzungen{$1}/g; print $datei;
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
The regular expression:
(?-imsx:(?<=\$)(.+?)(?![A-Za-z0-9]))
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):
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
\$ '$'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
.+? any character except \n (1 or more times
(matching the least amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
[A-Za-z0-9] any character of: 'A' to 'Z', 'a' to
'z', '0' to '9'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
/\$([A-Za-z0-9_]+)/g
my @platzhalter = $string =~ /\$([A-Z].\w+)/g;