gpt4 book ai didi

java - 如何制作正则表达式以查看字符串是否包含某个字母

转载 作者:搜寻专家 更新时间:2023-11-01 03:06:31 25 4
gpt4 key购买 nike

在一个网站上,我找到了“The quick brown fox jumps over the lazy dog”的一些替代方案,我决定编写一个小程序来检查替代方案是否有效。

对于那些感兴趣的人,我编写了以下程序(使用 this post 的文件阅读器思想)来检查文件中的句子:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TestClass {
public static void main(String... aArgs) {
TestClass tc = new TestClass();
try {
String[] pieces = tc.splitFile("/home/user2609980/Desktop/text");
for (String line : pieces) {
if (line.contains("a") &&
line.contains("b") &&
line.contains("c") &&
line.contains("d") &&
line.contains("e") &&
line.contains("f") &&
line.contains("g") &&
line.contains("h") &&
line.contains("i") &&
line.contains("j") &&
line.contains("k") &&
line.contains("l") &&
line.contains("m") &&
line.contains("n") &&
line.contains("o") &&
line.contains("p") &&
line.contains("q") &&
line.contains("r") &&
line.contains("s") &&
line.contains("t") &&
line.contains("u") &&
line.contains("v") &&
line.contains("w") &&
line.contains("x") &&
line.contains("y") &&
line.contains("z")) {
System.out.println("Matches: " + line);
} else {
System.out.println("Does not match: " + line);
}
}

} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

public String[] splitFile(String file) throws IOException {

BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
sb.append(line);
sb.append('\n');
line = br.readLine();
}
String everything = sb.toString();
String[] pieces = everything.split("\n");
return pieces;
} finally {
br.close();
}

}
}

这是输出:

Matches: The quick brown fox jumps over the lazy dog
Does not match: Pack my box with five dozen liquor jugs.
Matches: Several fabulous dixieland jazz groups played with quick tempo.
Does not match: Back in my quaint garden, jaunty zinnias vie with flaunting phlox.
Does not match: Five or six big jet planes zoomed quickly by the new tower.
Matches: Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.
Matches: I quickly explained that many big jobs involve few hazards.
Does not match: Jay Wolf is quite an expert on the bass violin, guitar, dulcimer, ukulele and zither.
Matches: Expect skilled signwriters to use many jazzy, quaint old alphabets effectively.
Matches: The wizard quickly jinxed the gnomes before they vaporized.

我想从两个方面改进这个程序。第一个,也是我的问题,是如何编写更高效的代码,而不是分别检查字母表中的每个字母。我怎样才能做出类似的东西:

line.Contains([regex])

如果可能的话?

奖金问题是我如何制作这个程序,以便它准确地打印出它不匹配的地方。当然,我可以为每个字母做一个 if-else,但我希望有更漂亮的方法。

感谢您的关注,我期待着阅读您可能的回复。

最佳答案

在我看来最简单的是使用这样的循环:

boolean allChars = true;
String uline = line.toUpperCase();
for (char c='A'; c<='Z'; c++) {
if (uline.indexOf(c) < 0) {
allChars = false;
break;
}
}

即运行从 65 (A) 到 90 (Z) 的循环,并检查输入字符串中每个字符是否存在。

关于java - 如何制作正则表达式以查看字符串是否包含某个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com