Guest Graf HerschelVor der Änderung will ich auch die Position, falls < x ändere ich nicht.
Guest Graf HerschelVor der Änderung will ich auch die Position, falls < x ändere ich nicht.
1
2
3
4
String length: 24000
Rate reverse_regex rindex_substr
reverse_regex 19321/s -- -80%
rindex_substr 95467/s 394% --
1
2
3
4
String length: 12000
Rate reverse_regex rindex_substr
reverse_regex 39821/s -- -66%
rindex_substr 118153/s 197% --
1
2
3
4
5
6
7
my $line="abcdefABCDEF";
my $subs='K';
$line =~ /([^CBf]*)$/;
my $pos = $-[0] - 1;
$line =~ s/(?<=.{$pos})(.)/\u$subs/;
print("POS: [$pos] $line\n");
Lookbehind longer than 255 not implemented in regex m/(?<=.{2396})./ at t.pl line 51.
1
2
3
4
5
6
7
Smartmatch is experimental at backwards_replace.pl line 43.
String Length: 2500
Rate pure_regex for_smartmatch reverse_regex rindex_substr
pure_regex 752/s -- -100% -100% -100%
for_smartmatch 190934/s 25277% -- -35% -46%
reverse_regex 292322/s 38753% 53% -- -18%
rindex_substr 356174/s 47240% 87% 22% --
1 2 3 4 5 6 7 8 9 10 11
my $min_idx = 5; # EDIT: Den $min_idx eingebaut my $string = "abcdefABCDEF"; for (my $i = length($string)-1; $i >= $min_idx; $i--) { if (substr($string, $i, 1) ~~ ['C', 'B', 'f']) { substr($string, $i, 1) = 'K'; last; } } say $string;
1
2
3
4
5
6
7
Smartmatch is experimental at backwards_replace.pl line 43.
String Length: 2500
Rate pure_regex for_smartmatch reverse_regex rindex_substr
pure_regex 752/s -- -100% -100% -100%
for_smartmatch 190934/s 25277% -- -35% -46%
reverse_regex 292322/s 38753% 53% -- -18%
rindex_substr 356174/s 47240% 87% 22% --
@cps = unpack "U*", $str;
1
2
3
$str =~ /([^CBf\s)])*$/; # match hinten (sind in Wirklichkeit mehr Zeichen)
$pos=$-[0] - 1; # ist -1 wenn kein match, ansonsten $str[$pos]
$csp=substr($`,-1,1); # welches Zeichen gefunden wurde
QuoteWenn bei
$str =~ /([^CBf\s)])*$/;
der gesuchte an letzter Stelle steht, findet er ihn nicht.
1
2
3
4
5
6
7
my $str="01234567890123456789\n";
my $len=length($str);
$str =~ /([^49\s])*$/;
my $pos=$-[0] - 1;
my $csp=substr($`,-1,1);
print("CSP:\"$csp\" POS:$pos LEN:$len\n");
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 'C'|'B'|'f', durch 'K' ersetzen # Hashslice für die Ersetzungen anlegen my %hx = (); @hx{unpack "UUU", "CBf"} = unpack "UUU", "KKK"; my $string = "abcdefABCDEF"; # Array mit den Codepoints erzeugen my @cps = unpack "U*", $string; # nun gehts durch das Array # Ersetzen ab bestimmter Position z.B. 3 foreach my $i (3 .. scalar @cps -1 ){ $cps[$i] = $hx{$cps[$i]} || $cps[$i]; } # Ergebnis $, = "\n"; print $string, pack("U*", @cps); abcdefABCDEF abcdeKAKKDEF
my $string = "abcdefABCDEF" x 10000;
QuoteDiesen string möchte ich von hinten beginnend durchsuchen und nur den ersten Character, wenn 'C'|'B'|'f', durch 'K' ersetzen.
1
2
3
4
5
String length: 2400
Rate unpack_pack rindex_substr reverse_regex
unpack_pack 2327/s -- -98% -99%
rindex_substr 146161/s 6182% -- -13%
reverse_regex 168659/s 7149% 15% --
1
2
3
4
5
String length: 24000
Rate unpack_pack reverse_regex rindex_substr
unpack_pack 241/s -- -99% -100%
reverse_regex 19140/s 7838% -- -80%
rindex_substr 93699/s 38760% 390% --
1
2
3
4
5
6
String length: 2400
Rate split_tr_join unpack_pack rindex_substr reverse_regex
split_tr_join 880/s -- -62% -99% -99%
unpack_pack 2337/s 166% -- -98% -99%
rindex_substr 146161/s 16513% 6155% -- -11%
reverse_regex 163840/s 18522% 6912% 12% --
$cps[$i] = delete $hx{$cps[$i]} || $cps[$i];
1 2 3 4 5 6 7 8 9 10 11 12
use Benchmark qw(cmpthese); my $s = "€" x 1000; cmpthese(10000, { 'U Schablone' => sub{ pack("U*", unpack("U*", $s)) }, 'C Schablone' => sub{ pack("C*", unpack("C*", $s)) } }); Rate U Schablone C Schablone U Schablone 2026/s -- -38% C Schablone 3282/s 62% --
2018-12-13T10:51:17 RaubtierCode (perl): (dl )$string =~ s/^.* \K [CBf]/K/xs;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Smartmatch is experimental at backwards_replace.pl line 58.
Original String: abcdefABCDEF
unpack_C_pack: abcdefABKDEF
regex_match_start: abcdefABKDEF
rindex_substr: abcdefABKDEF
regex_match_end: abcdefABKDEF
unpack_U_pack: abcdefABKDEF
reverse_regex: abcdefABKDEF
rindex_substr_max: abcdefABKDEF
regex_with_K: abcdefABKDEF
for_smartmatch: abcdefABKDEF
Using new string for Benchmark.
String Length: 5000
Rate regex_match_end unpack_U_pack unpack_C_pack reverse_regex regex_match_start for_smartmatch regex_with_K rindex_substr rindex_substr_max
regex_match_end 373/s -- -91% -93% -100% -100% -100% -100% -100% -100%
unpack_U_pack 4267/s 1045% -- -17% -98% -98% -98% -98% -99% -99%
unpack_C_pack 5168/s 1287% 21% -- -97% -97% -97% -98% -99% -99%
reverse_regex 170836/s 45745% 3904% 3205% -- -10% -11% -24% -52% -80%
regex_match_start 189150/s 50659% 4333% 3560% 11% -- -1% -16% -47% -77%
for_smartmatch 190934/s 51138% 4375% 3594% 12% 1% -- -15% -46% -77%
regex_with_K 224878/s 60247% 5171% 4251% 32% 19% 18% -- -37% -73%
rindex_substr 355071/s 95185% 8222% 6770% 108% 88% 86% 58% -- -57%
rindex_substr_max 835106/s 224004% 19473% 16058% 389% 342% 337% 271% 135% --
1 2 3 4 5 6 7 8 9 10 11 12 13
sub rindex_substr_max { my ( $string, ) = @_; my $max = 0; for my $x ( qw( B C f ) ) { my $i = rindex $string, $x; $max = $i if $i > $max; } substr( $string, $max, 1 ) = "K"; return $string; }
1 2 3 4 5 6 7 8 9 10 11 12 13
sub rindex_substr { my ( $string, ) = @_; my %pos; for my $x ( qw( B C f ) ) { $pos{$x} = rindex($string,$x); } my $replace = ( sort { $pos{$b} <=> $pos{$a} } keys %pos )[0]; substr( $string, $pos{$replace}, 1 ) = "K"; return $string; }