检查两个正则表达式是否匹配java中的相同字符串
我有两个正则表达式(简单的示例:“[0-9]+”和“[0123456789]+”)。我想看看它们是否与完全相同的输入匹配。 java中有没有内置函数可以进行此检查?如果没有,是否有一个相对简单的算法来进行检查?谢谢!
I have two regular expressions (simple example: "[0-9]+" and "[0123456789]+"). I'd like to see if they match exactly the same inputs. Is there a built-in function for doing this check in java? If not, is there a relatively easy algorithm for doing the check? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有一种算法方法可以检查正则表达式是否相等,尽管它很复杂。方法如下:
There actually is an algorithmic way to check for regex equality, although it's complicated. Here's how:
首先,它是完全相同的。其次,我无法想象内置函数可以满足您的需求。想想:您实际上想要将正则表达式与多个输入进行匹配。什么输入?随机字符串?在这种情况下,您的随机字符串仅包含数字的机会是非常规律的。
我可以稍微改变一下你的问题。这是我的版本。
*我有 2 个正则表达式,想验证它们的功能是否相同。 *
这个问题很有道理。在这种情况下,我可以使用一个流行的单元测试框架(例如 JUnit 或 TestNG)编写一系列单元测试,并对这两个正则表达式运行相同的测试。我每次都期待相同的结果。但我必须自己写字符串。例如,
字符串,等等
First, it is exactly the same. Second, I cannot imagine built-in function that does what you want. Think: you actually want to match the regex against several inputs. What inputs? Random strings? In this case the chance that your random string contains digits only is very law.
I can a little bit change your question. Here is my version.
*I have 2 regular expressions and want to verify that they function equally. *
This question makes sense. In this case I can write a series of unit test using one of popular unit test frameworks (e.g. JUnit or TestNG) and run the same tests against these 2 regexes. I am expecting the same results every time. But I have to write the strings myself. For example
etc, etc