例如,以下是有效的数学表达式:
a * b + c
-a * (b / 1.50)
(apple + (-0.5)) * (boy - 1)
这些是无效的数学表达式:
--a *+ b @ 1.5.0 // two consecutive signs, two consecutive operators, invalid operator, invalid number
-a * b + 1) // unmatched parentheses
a) * (b + c) / (d // unmatched parentheses
我在匹配浮点数方面没有问题,但在圆括号匹配方面有困难。有什么想法吗?如果有比正则表达式更好的解决方案,我也会接受。但最好使用正则表达式。
========
编辑:
我想对我选择的“接受的答案”做一些评论,希望有同样问题并找到这个帖子的人不会被误导。
我认为有几个答案是“接受的”,但我不知道哪一个是最好的。所以我(几乎)随机地选择了被接受的答案。除了公认的答案之外,我还推荐阅读Guillaume Malartre的答案。他们都为我的问题提供了实际的解决方案。要获得一个比较严格的/理论上的答案,请阅读David Thornley在接受的答案下的评论。正如他提到的,Perl对正则表达式的扩展(源自正则语言)使其“不规则”。(我在问题中没有提到语言,所以大多数回答者假定Perl实现了正则表达式--可能是最流行的实现。当我发布我的问题时,我也是。)
如果我上面说错了什么,请纠正我。
发布于 2010-04-08 03:42:14
用正则表达式匹配括号是很有可能的。
下面是一个Perl脚本,它将解析任意深度匹配的括号。虽然它会在外部抛出不匹配的括号,但我并不是专门为验证括号而设计的。它将解析任意深度的括号,只要它们是平衡的。然而,这将帮助您开始。
在regex和使用regex时,关键都是递归。使用它,我相信你可以让它也标记不匹配的prens。我认为如果你捕获这个正则表达式丢弃的东西并计算括号(即测试非匹配文本中的奇数括号),你就得到了无效的、不平衡的括号。
#!/usr/bin/perl
$re = qr /
( # start capture buffer 1
\( # match an opening paren
( # capture buffer 2
(?: # match one of:
(?> # don't backtrack over the inside of this group
[^()]+ # one or more
) # end non backtracking group
| # ... or ...
(?1) # recurse to opening 1 and try it again
)* # 0 or more times.
) # end of buffer 2
\) # match a closing paren
) # end capture buffer one
/x;
sub strip {
my ($str) = @_;
while ($str=~/$re/g) {
$match=$1; $striped=$2;
print "$match\n";
strip($striped) if $striped=~/\(/;
return $striped;
}
}
while(<DATA>) {
print "start pattern: $_";
while (/$re/g) {
strip($1) ;
}
}
__DATA__
"(apple + (-0.5)) * (boy - 1)"
"((((one)two)three)four)x(one(two(three(four))))"
"a) * (b + c) / (d"
"-a * (b / 1.50)"
输出:
start pattern: "(apple + (-0.5)) * (boy - 1)"
(apple + (-0.5))
(-0.5)
(boy - 1)
start pattern: "((((one)two)three)four)x(one(two(three(four))))"
((((one)two)three)four)
(((one)two)three)
((one)two)
(one)
(one(two(three(four))))
(two(three(four)))
(three(four))
(four)
start pattern: "a) * (b + c) / (d"
(b + c)
start pattern: "-a * (b / 1.50)"
(b / 1.50)
发布于 2010-04-08 03:26:45
正则表达式只能用于识别正则语言。数学表达式的语言不是正则的;你需要实现一个实际的解析器(例如LR)才能做到这一点。
发布于 2010-04-08 03:27:05
使用下推自动机匹配括号http://en.wikipedia.org/wiki/Pushdown_automaton (或仅使用堆栈;-)
堆栈解决方案的详细信息:
while (chr available)
if chr == '(' then
push '('
else
if chr == ')' then
if stack.elements == 0 then
print('too many or misplaced )')
exit
else
pop //from stack
end while
if (stack.elements != 0)
print('too many or misplaced(')
甚至很简单:只保留一个计数器而不是堆栈。
https://stackoverflow.com/questions/2595254
复制相似问题