Leser: 1
|< 1 2 >| | 16 Einträge, 2 Seiten |
my $pos=length((split(/\d+:/,$string,2))[0]);
"hallo welt" =~ /[wxyz]/ and print $-[0];
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 the perlfunc manpage. 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.
D:\meinPfad\datei.c 881: meine Nachricht mit Datei2.h 991: blabla
1
2
3
my $string = 'D:\meinPfad\datei.c 881: meine Nachricht mit Datei2.h 991: blabla';
my ($match) = $string =~ /^(.*?\d+:)/;
print $match;
my ($match) = $string =~ /^(.*?)\s?\d+:/;
1
2
3
4
5
6
7
8
sub index_regex {
my ($string, $regex) = @_;
$string =~ $regex or return -1;
return $-[0];
}
my $index = index_regex("hallo Welt", qr/[A-Z]/);
print "Whoah, ein Grossbuchstabe bei $index\n" if $index != -1;
|< 1 2 >| | 16 Einträge, 2 Seiten |