【发布时间】:2021-07-30 15:37:18
【问题描述】:
在 SO 和其他地方有很多用于匹配括号或括号对的解决方案,但我找不到或想出排除交错项目的解决方案。这个挑战的解决方案是什么:
Write a regular expression for a string containing any number of X
and single pairs of < > and { } which may be nested but not
inter-leaved. For example these strings are allowed:
XXX<XX{X}XXX>X
X{X}X<X>X{X}X<X>X
But these are not allowed:
XXX<X<XX>>XX
XX<XX{XX>XX}XX
这完全可以用正则表达式(有限自动机)来完成吗?不需要下推自动机吗?
除了不交错之外,请注意对单对的要求,这意味着嵌套可能只有一层深度,不同的括号类型,如图所示。
没有偏好使用哪种正则表达式引擎/语言。
【问题讨论】:
-
@AKSingh OP 应该写一个正则表达式。使用堆栈不是正则表达式。
-
我想到了like this。
标签: regex parsing state-machine regular-language