如何驗證如果碼塊,如在一個構建體:檢查代碼塊是否在Java中正確嵌套?
{
// Any amount of characters that aren't '{' or '}'
}
正確嵌套,優選具有正則表達式?
{} {
{} {}
} // Properly nested
{{
{{}}
} {} // Not properly nested
As referred to from this thread,方法,如遞歸和平衡組不能在這裏適用,因爲regular expression constructs中不存在Java Pattern。
我不會使用這類問題的正則表達式。我可能會使用堆棧。 – 2014-09-01 15:02:34
@HovercraftFullOfEels用戶輸入是一個String。在這種情況下有沒有辦法使用堆棧? – Unihedron 2014-09-01 15:04:05
是的。我不是這方面的專家,但是不存在這樣的解析器嗎? – 2014-09-01 15:05:11