s********s 发帖数: 4011 | 1 regular expression用的不熟,请教一下各位高手
有以下输入
Print Media="Wh Paper", Type="A4 Size", Tray="Tray 1",PDL=PS, Orient=LS;
需要把item 和 value 分别extract 出来
string Item[]={"Print Media", "Type", "Tray", "PDL", "Orient"};
string Value[]={"Wh Paper", "A4 Size", "Tray 1", "PS", "LS"};
因为分隔符不统一,而且value有得有引号,有的没有,我可以
每种分别判断,感觉不太有效率,有没有
什么更有效的方法把值取出来?多谢! | j********g 发帖数: 88 | | k****z 发帖数: 550 | 3 If use Perl, first split then match with /^\s*(.+)=(\"?)(.+)\2$/
and move the trailing ";" from the last element. It works but may not be
very economic.
*************************************************
#!/usr/bin/perl -w
my $str = 'Print Media="Wh Paper", Type="A4 Size", Tray="Tray 1",PDL=PS,
Orient=LS;';
my @substr = split /,/, $str;
print "substr = @substr\n";
my @term;
my @value;
for (@substr) {
/^\s*(.+)=(\"?)(.+)\2$/; # \2 means match \" if there is \" before
【在 s********s 的大作中提到】 : regular expression用的不熟,请教一下各位高手 : 有以下输入 : Print Media="Wh Paper", Type="A4 Size", Tray="Tray 1",PDL=PS, Orient=LS; : 需要把item 和 value 分别extract 出来 : string Item[]={"Print Media", "Type", "Tray", "PDL", "Orient"}; : string Value[]={"Wh Paper", "A4 Size", "Tray 1", "PS", "LS"}; : 因为分隔符不统一,而且value有得有引号,有的没有,我可以 : 每种分别判断,感觉不太有效率,有没有 : 什么更有效的方法把值取出来?多谢!
| t*********s 发帖数: 5 | 4 it depends on what can show up in the quotation marks,
for example:
Media description="for example, '1' + '1' = '2';"
Is the above valid in your applications??
*** if no [,;=] in the quoted stuff, then it might be:
(?!\s)([^=,]+)=(["']?)([^,;]+)\2
check on $1(attr) and $3(value) for the attr-value pairs.
*** If on the other hand, you can have any graphic chars
enclosed in the RHS quotation marks, then
(?!\s)([^=,]+)=(?:(["'])(.+?)\2|(\S+))
check on $1 for attrs and the concatenation of
$3 a |
|