Hallo,
Ich habe Werte in einer Datei mit Single oder Double Quotes wie:
'.VAL1'
'VAL1','VAL2'# SYNTAX parameter2 = { VAL3 | VAL4 | VAL1 [, VAL2] | VAL5 [, VAL6] }
'VAL1'# SYNTAX parameter3 = { VAL1 | VAL2 | VAL3 | VAL4 }
"VAL3"
Es sollen nur die äussersten Quotes bestehen bleiben , nur x-beliebige Quotes innerhalb des Stringes gelöscht werden :
Von Eintrag Zeile : 'VAL1
',
'VAL2' bleiben die äusseren Quotes bestehen und Ausgabe wird zu
zu 'VAL1,VAL2'
Meine Idee :
Die Quotes zu zählen und dann ab Position zu löschen :
Eine Vorlage von
https://perldoc.perl.org/perlfaq4 habe ich genommen und
wegen Quotes diese Variante gewählt ( keine Index Funktion )
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
use strict;
use warnings;
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;
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 ) )
{
print substr( $string, $position - 1 , 1 ) . "\n";
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
{
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)
{
$pos_c_str = pos($str);
if (++$count_str_qu == 1)
{
$first_c_pos = $pos_c_str;
}
else
{
$last_c_pos = $pos_c_str;
}
}
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;
}
https://perldoc.perl.org/perlfaq4
Last edited: 2021-03-24 20:49:19 +0100 (CET)