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

标签 java regex contains

在一个网站上,我找到了“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/

相关文章:

c++ - 尝试在 Linux 上使用 Clang++ 编译 c++11 正则表达式教程时出错

regex - Excel VBA - 查找带有通配符的字符串

javascript - 如何检查字符串是否包含 JavaScript 中的特定单词

Java ArrayList.contains() 和 add() 方法

java - 将 2 方法与另一个方法同步,但彼此之间不同步

java - 我应该在 grails 2.0 中使用哪个 paypal/payment 插件?

Java线程在多个计时器上触发

javascript - 如何使用正则表达式限制 0 - 100 并且在 Javascript 中不输入空白?

java - android:显示从sqlite到AlertDialog的数据

java - HashMap 做 containsKey 的方式不符合预期