Schrift
[thread]7425[/thread]

"Fehler" bei Regulären Ausdrücken: Fehlerhaftes Verhalten pattern match



<< >> 6 Einträge, 1 Seite
Gast Gast
 2005-11-03 10:53
#59641 #59641
Hallo zusammen,

mir ist ein seltsames Verhalten in Perl aufgefallen.
Wird auf eine Variable das selbe Pattern-Match in
mehreren IF-Abfragen hintereinander durchgeführt (mit globaler Erkennung!), so findet Perl den Match lediglich in jeder zweiten Abfrage!!!!
Ist euch so ein Verhalten schon mal aufgefallen? Und kann
jemand erklären aus welchen Gründen das so ist?

Das Programm lautet:

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
# /* INCLUDE */
use strict;

my $sFeld = "ParamSet";

if($sFeld =~ m/ParamSet/g)
{
print "1 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "2 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "3 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "4 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "5 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "6 mal gefunden\n";
}
if($sFeld =~ m/ParamSet/g)
{
print "7 mal gefunden\n";
}


Die Ausgabe beim Ausführen des Programms dann:
1 mal gefunden
3 mal gefunden
5 mal gefunden
7 mal gefunden

Vielen Dank schon mal für eure Hilfe!!\n\n

<!--EDIT|esskar|1131009924-->
esskar
 2005-11-03 11:25
#59642 #59642
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
witzig!
Dubu
 2005-11-03 11:38
#59643 #59643
User since
2003-08-04
2145 Artikel
ModeratorIn + EditorIn

user image
[quote=perldoc perlop,Regexp Quote-Like Operators]
The "/g" modifier specifies global pattern matching--that is, matching as many times as possible within the string.  How it behaves depends on the context.  In list context, it returns a list of the substrings matched by any capturing parentheses in the regular expression.  If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern.

In scalar context, each execution of "m//g" finds the next match, returning true if it matches, and false if there is no further match.  The position after the last match can be read or set using the pos() function; see "pos" in perlfunc.   A failed match normally resets the search position to the beginning of the string, but you can avoid that by adding the "/c" modifier (e.g. "m//gc").  Modifying the target string also resets the search position.
[/quote]
betterworld
 2005-11-03 12:53
#59644 #59644
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
pq
 2005-11-03 13:23
#59645 #59645
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
@shorty: habe termin im kalender entfernt, ich nehme jetzt mal an, das war ein versehen,
oder möchtest du zu einem posting ein treffen organisieren?
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
pq
 2005-11-03 13:29
#59646 #59646
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=Guest,03.11.2005, 09:53]Ist euch so ein Verhalten schon mal aufgefallen? Und kann
jemand erklären aus welchen Gründen das so ist?
Code: (dl )
if($sFeld =~ m/ParamSet/g)
[/quote]
ich würde mich eher fragen, wieso du zum einen wissen möchtest, ob
ParamSet enthalten ist, aber zum anderen ein /g dranhängst,
wenn du eh nur einmal matchen willst.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
<< >> 6 Einträge, 1 Seite



View all threads created 2005-11-03 10:53.