Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算大型文本文件中的单个单词时的引号问题_Java_Regex - Fatal编程技术网

Java 计算大型文本文件中的单个单词时的引号问题

Java 计算大型文本文件中的单个单词时的引号问题,java,regex,Java,Regex,我需要创建代码来计算.txt文件中的单个单词。格式必须类似于: the - 10 text - 1 has - 5 etc. 我遇到了一个似乎无法解决的问题: 文本使用撇号表示quoes,因此我的代码解析的单词像“不,不看”和“不”一样。我不知道怎么解决这个问题 这是代码的特定部分。我必须在分隔符中使用正则表达式 static int findAndCountWords (Scanner scanner, String[] words, int [] freqs) { assert (

我需要创建代码来计算.txt文件中的单个单词。格式必须类似于:

the - 10
text - 1
has - 5
etc.
我遇到了一个似乎无法解决的问题: 文本使用撇号表示quoes,因此我的代码解析的单词像“不,不看”和“不”一样。我不知道怎么解决这个问题

这是代码的特定部分。我必须在分隔符中使用正则表达式

static int findAndCountWords (Scanner scanner, String[] words, int [] freqs)
{
    assert (words != null)&&(freqs != null): "findAndCountWords doesn't work.";
    int nr=0;
    while (scanner.hasNext())
    {   
        String word = scanner.next();
        word = word.toLowerCase();
        scanner.useDelimiter("[^a-z]");
        //|[^a-z]+[\\'][^a-z]+
        if (updateWord(word, words, freqs, nr))
        nr++;
    }
    return nr;
}

我会先从你的话中删掉任何撇号

您可以使用Apache commons执行此操作:

str = StringUtils.stripStart(str,"'")
或者你的媒人:

Pattern pattern = Pattern.compile("(?:^')|(?:'$)); // starts or ends with apostrophe
str = pattern.matcher(str).replaceAll(""); // not anymore

(我没有测试代码,可能是一些bug)

添加撇号怎么样
.useDelimiter(“[^a-z']”)
?您可以开始使用Delimiter
“\\W*\\s+\\W*”