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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#!/usr/bin/perl -w use strict; use warnings; # Hex Zeichen # 22 " # 27 ' my $string = "'VAL1','VAL2',\"VALX\""; $string = "\"\""; my $char_quotes = "[\x22\x27]"; my $count = 0; my $first = 0; my $last = 0; my $position = 0; $count = count_occurrence ($string, $char_quotes); print "count<$count> of <$char_quotes> in <$string>\n"; my $string_out = $string; my $cnt_qu_str = 1; # Nur wenn mehrere Quotes gefunden wurden => Löschen if ( $count > 2 ) { ( $first, $last ) = find_str_pos ( $string ); print "first: $first\n"; print "last: $last\n"; while( $string =~ m/[\x22\x27]/g) { $position = pos( $string ); if ( ( $position != $first ) && ( $position != $last ) ) { # Information print substr( $string, $position - 1 , 1 ) . "\n"; # Löschen if (substr($string_out, $position - $cnt_qu_str , 1 ) =~ $char_quotes ) { substr($string_out, $position - $cnt_qu_str , 1 ) = ""; ++$cnt_qu_str ; } } } print "Test-Ausgabe: $string_out\n"; } sub find_str_pos { # Regex to search for substring # using m modifier my ($str) = @_; my $pos_c_str = 0; my $first_c_pos = 0; my $last_c_pos = 0; my $count_str_qu = 0; while( $str =~ m/[\x22\x27]/g) { # Finding the pos_c_str of substr # using pos() function $pos_c_str = pos($str); if (++$count_str_qu == 1) { $first_c_pos = $pos_c_str; # print "$count\n"; } else { $last_c_pos = $pos_c_str; } # print "$pos_c_str\n"; } return ( $first_c_pos, $last_c_pos ); } sub count_occurrence{ my ($str, $char_quote) = @_; my $count_qu = 0; my $len = length($str); $str =~ s/$char_quote//g; my $len_new = length($str); $count_qu = $len - $len_new; return $count_qu; }
2021-03-24T19:47:54 bora99Es sollen nur die äussersten Quotes bestehen bleiben , nur x-beliebige Quotes innerhalb des Stringes gelöscht werden
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl use strict; use warnings; my @strings = ( "'VAL1','VAL2',\"VALX\"" , "foo 'VAL1','VAL2',\"VALX\"" , "foo 'VAL1','VAL2',\"VALX\" bar" , "'VAL1','VAL2',\"VALX\" bar" ); for my $string (@strings) { my ($prefix, $innerstring, $postfix) = $string =~ /(.*?["'])(.+)(["'].*)/; $innerstring =~ s/["']//g; print $prefix, $innerstring, $postfix, "\n"; }
1 2 3 4 5 6 7 8
#!/usr/bin/perl use strict; use warnings; $_ = "\"VAL\", \"VAL\""; print $_.$/; s/(?<!\A)("|')(?!\Z)//g; print $_.$/;