【问题标题】:How do I count parenthesis and curly brackets in a text file in Java?如何计算 Java 文本文件中的括号和大括号?
【发布时间】:2016-04-05 15:29:53
【问题描述】:

我正在尝试计算程序在文本文档中找到的子字符串的数量。文本文档:

# Data Value 0:
dataValue(0) {
  x: -3
  y: +9
  width: 68
  height: 25
}

在我的程序中,我试图打印 'dataValue(' 出现的次数。我遇到了括号问题。根据我在寻找解决方案时发现的内容,我必须转义括号。这是正确吗?但是,我发现当我这样做时,程序将其解释为 'dataValue\(' 而不是 'dataValue('。结果,找不到匹配项。我可以解决这个问题吗?如果是这样,任何帮助都会不胜感激。

主要方法:

static String fileContent = "";

public static void main(String args[]) {

    fileContent = getFileContent("/Users/Rane/Desktop/search.txt");
    System.out.println(countSubstring(fileContent, "dataValue\\("));

}

getFileContent() 方法:

    public static String getFileContent(String filePath) {

    File textFile = new File(filePath);
    BufferedReader reader = null;

    String content = "";
    String currentLine = "";

    if(textFile.exists()) {
        try {

            reader = new BufferedReader(new FileReader(textFile));

            currentLine = reader.readLine();
            while(currentLine != null) {
                content = content + currentLine + "\n";;
                currentLine = reader.readLine();
            }

        } catch(Exception ext) {
            ext.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch(Exception ext) {
                ext.printStackTrace();
            }
        }

    } else {
        System.out.println("[WARNING]: Text file was not found at: " + filePath);
    }


    return content;
}

countSubstring() 方法:

static int countSubstring(String search, String substring) {

    int occurrences = 0;
    System.out.println(substring);

    search = search.toLowerCase();
    substring = substring.toLowerCase();

    while(search.indexOf(substring) > -1) {
        search = search.replaceFirst(substring, "");
        occurrences ++;
    }

    return occurrences;

}

控制台输出:

dataValue\(
0

提前致谢!

【问题讨论】:

    标签: java escaping substring


    【解决方案1】:

    对于indexOf,您不需要转义(indexOf 接受一个字符串作为参数,而不是像其他一些方法那样的正则表达式。

    另一个注意事项,如果您只想计算事物,则需要更改此设置:

    while(search.indexOf(substring) > -1) {
        search = search.replaceFirst(substring, "");
        occurrences ++;
    }
    

    收件人:

    int index = -1;
    
    while((index = search.indexOf(substring, ++index)) > -1) 
        occurances++;
    

    indexOf 产生所提供子字符串的位置。我们正在使用一个重载版本,它也从哪里开始匹配。我们需要这样做以避免不断找到相同的元素,从而使其成为无限循环。

    【讨论】:

    • 感谢您的回答。我只是有一个快速的后续问题。为什么需要起始位置?在我的原始代码中,如果找到子字符串,我会替换它。这不会防止无限循环吗?再次感谢。
    • 起始位置用于避免命中之前的匹配,也可以通过从字符串中移除之前的匹配来实现。问题是当你改变一个字符串时,你正在创建一个新字符串。根据字符串的大小,此操作可能非常昂贵。
    【解决方案2】:

    那是因为你混合使用了搜索字符串:

    • indexOf() 接受一个普通的搜索字符串
    • replaceFirst() 采用正则表达式

    如果您只想提供纯字符串,可以使用 Pattern.quote() 引用该字符串以用作正则表达式。

    更好的是,不要浪费时间替换搜索字符串,只需继续搜索,使用indexOf() 用于简单搜索字符串,或使用find() 用于正则表达式:

    // Using indexOf() with a plain search string
    int start = -1, count = 0;
    while ((start = search.indexOf(substring, ++start)) != -1)
        count++;
    return count;
    
    // Using find() with a regular expression search string
    Matcher m = Pattern.compile(substring).matcher(search);
    int count = 0;
    while (m.find())
        count++;
    return count;
    

    【讨论】:

      猜你喜欢
      • 2019-05-28
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 2012-11-13
      • 1970-01-01
      相关资源
      最近更新 更多