Java中的正则表达式匹配
我正在尝试在Java中生成一个正则表达式匹配,它接受包含3和7的所有数字(以任何顺序,即所有3都应该至少有一个7,反之亦然)整数集。到目前为止,我已经编写了下面的代码,但是我无法获得正确的输出。任何形式的帮助将不胜感激:
class Main {
public static void main (String[] args) throws java.lang.Exception {
System.out.println("333333".matches("[[3][7]+]*") ? "Yes" : "No");
}
}
在这里,我应该得到输出为“否”,因为给定的字符串只是 3,而不是 3,并且至少有一个 7。
I am trying to generate a regular expression match in Java, that accepts all numbers that contain 3 and 7 (in any order, ie there should be at least one 7 for all 3's and vice versa) over the set of integers. So far, I have written the code below, however I am not able to get the correct output. Any kind of help will be appreciated:
class Main {
public static void main (String[] args) throws java.lang.Exception {
System.out.println("333333".matches("[[3][7]+]*") ? "Yes" : "No");
}
}
Here, I should be getting the output as NO, since the given string is only of 3's not of 3 and at least one 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定是否需要正则表达式。为什么不使用以下内容:
I'm not sure if a regex is necessary. Why not use the following:
根据您的描述,这应该足够了:
由于正则表达式检查字符序列,因此此练习变得不必要的复杂。
From your description, this should be sufficient:
Since a regex examines a sequence of characters, this exercise becomes unnecessarily complicated.
^(\d*3\d*7\d*)|(\d*7\d*3\d*)$
我想。^(\d*3\d*7\d*)|(\d*7\d*3\d*)$
I think.