這是我所看到的:
'100=product(getProduct_abc) in (Xyz)'
Group1 match = '100'
Group2 match = '='
Group3 match = 'product(getProduct_abc) in (Xyz)'
'product(getProduct_abc) in (Xyz)'
^
Fails here on Group1 match because parenthesis are not included in this group
通過強制1,2,3組比賽的最後一次出現在字符串中的修復情況。
修復/重寫等價Group1匹配和分離組,它們可以重新組合以強制最後的匹配成爲可能。
rxP1 = '(?:-?[\w()]+\ *)';
rxP2 = '(?:!=|<=|>=|=|<|>| not in | in | not like | like)';
rxP3 = '(?:.*?)';
rxAll = /(?:$rxP1$rxP2$rxP3)*($rxP1)($rxP2)($rxP3)$/;
在Perl:
use strict;
use warnings;
my @samples = (
'product(getProduct_abc) in (Xyz1)',
'100=product(getProduct_abc) in (Xyz2)',
'100 like = != not like >product(getProduct_abc) in (Xyz3)',
);
my $rxP1 = '(?:-?[\w()]+\ *)';
my $rxP2 = '(?:!=|<=|>=|=|<|>| not in | in | not like | like)';
my $rxP3 = '(?:.*?)';
for (@samples)
{
if (/(?:$rxP1$rxP2$rxP3)*($rxP1)($rxP2)($rxP3)$/) {
print "\n1 = '$1'\n";
print "2 = '$2'\n";
print "3 = '$3'\n";
}
}
輸出:
1 = 'product(getProduct_abc)'
2 = ' in '
3 = '(Xyz1)'
1 = 'product(getProduct_abc)'
2 = ' in '
3 = '(Xyz2)'
1 = 'product(getProduct_abc)'
2 = ' in '
3 = '(Xyz3)'
來源
2011-01-14 06:12:52
sln
它看起來就像你正在試圖做太多的一個正則表達式。分解並正確解析字符串。 – 2011-01-14 03:08:17