Java 如何查找字符串中的字数
我试图通过下一个正则表达式来查找字符串中的字数 “[\s+!,?。'@]”但由于某种原因,当我使用拆分方法时,它仍然将空白作为一个单词来计算,我不明白为什么 对于以下输入:Java 如何查找字符串中的字数,java,Java,我试图通过下一个正则表达式来查找字符串中的字数 “[\s+!,?。'@]”但由于某种原因,当我使用拆分方法时,它仍然将空白作为一个单词来计算,我不明白为什么 对于以下输入: He is a very very good boy, isn't he? 我得到: 11 He is a very very good boy isn t he 以及所需的输出: 10 He is a very very good boy isn t he 这是我的代码: import java.io.*; imp
He is a very very good boy, isn't he?
我得到:
11
He
is
a
very
very
good
boy
isn
t
he
以及所需的输出:
10
He
is
a
very
very
good
boy
isn
t
he
这是我的代码:
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String s=scan.nextLine();
int count = 0;
String [] tokens = s.split("[\\s+!,?._'@]");
System.out.println(tokens.length);
for(int i = 0; i<tokens.length;i++){
System.out.println(tokens[i]);
}
}
}
import java.io.*;
导入java.util.*;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类解决方案{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
字符串s=scan.nextLine();
整数计数=0;
字符串[]标记=s.split(“[\\s+!,?。@]”);
System.out.println(令牌长度);
对于(int i=0;i,正则表达式仅将逗号计算为一个分隔符,而不是“逗号及其后的空格”。此外,括号内仅+
“一个或多个”元字符不适用;它在括号内按字面意思解释
将+
移到括号外,以便括号内的任何内容都可以作为分隔符计数一次或多次
String[] tokens = s.split("[\\s!,?._'@]+");
这将把“”、“
计算为一个分隔符,这样输出就不会在“boy”和“isn”之间显示一个空行。正则表达式只将逗号计算为一个分隔符,而不是“逗号及其后的空格”。此外,您的唯一分隔符是“+
一个或多个”括号内的元字符不适用;它在括号内按字面解释
将+
移到括号外,以便括号内的任何内容都可以作为分隔符计数一次或多次
String[] tokens = s.split("[\\s!,?._'@]+");
这将把“,”
作为一个分隔符,这样输出就不会在“boy”和“is”之间显示一个空行。字符串“他是一个非常非常好的男孩,不是吗?”
包含一个“,”
;您的正则表达式将把它视为两个单独的匹配项,一个用于,“
,另一个用于”
。只需在正则表达式的末尾添加一个+
:
String [] tokens = s.split("[\\s+!,?._'@]+");
测试了11个匹配项,没有额外的+
,有10个匹配项。字符串“他是一个非常非常好的男孩,不是吗?”
包含一个”,“
”;您的正则表达式将把它视为两个单独的匹配项,一个用于,“
,另一个用于”
。只需在正则表达式的末尾添加一个+
:
String [] tokens = s.split("[\\s+!,?._'@]+");
测试了11个匹配项,没有额外的+
,有10个匹配项。您可以使用模式和匹配器来设置单词边界,如下所示。我建议不要使用拆分方法,因为如果您不知道字符串中存在哪些分隔符,可能会导致问题:
public static void main(String[] args) {
String s = "He is a very very good boy, isn't he?";
Pattern p = Pattern.compile("\\b[a-zA-Z]+\\b");
Matcher m = p.matcher(s);
int count = 0;
while (m.find()) {
System.out.println(m.group());
count++;
}
System.out.println(count);
}
O/p:
您可以使用Pattern
和Matcher
这样的单词边界。我建议不要使用拆分方法,因为如果您不知道字符串中存在哪些分隔符,可能会导致问题:
public static void main(String[] args) {
String s = "He is a very very good boy, isn't he?";
Pattern p = Pattern.compile("\\b[a-zA-Z]+\\b");
Matcher m = p.matcher(s);
int count = 0;
while (m.find()) {
System.out.println(m.group());
count++;
}
System.out.println(count);
}
O/p:
如果要使用Regex
,请使用模式
和匹配器
组合
String s = "He is a very very good boy, isn't he?";
String pattern = "(\\w+'?\\w*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(s);
int wordCount = 0;
while(m.find()){
System.out.println("Found word: " + m.group());
wordCount++;
}
System.out.println("Word count: " + wordCount);
给我输出:
Found word: He
Found word: is
Found word: a
Found word: very
Found word: very
Found word: good
Found word: boy
Found word: isn't
Found word: he
Word count: 9
我使用的正则表达式模式是:
(\\w+'?\\w*)
\\w+
匹配任何字母数字字符中的一个或多个,'?
匹配0或1'字符(用于诸如is not、dont等词),并且额外的\\w*
在字符后包含0个或更多的字母数字字符。可能有更好的正则表达式可供使用,但这对我有效。如果要使用正则表达式
,请使用模式
和匹配器
组合
String s = "He is a very very good boy, isn't he?";
String pattern = "(\\w+'?\\w*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(s);
int wordCount = 0;
while(m.find()){
System.out.println("Found word: " + m.group());
wordCount++;
}
System.out.println("Word count: " + wordCount);
给我输出:
Found word: He
Found word: is
Found word: a
Found word: very
Found word: very
Found word: good
Found word: boy
Found word: isn't
Found word: he
Word count: 9
我使用的正则表达式模式是:
(\\w+'?\\w*)
\\w+
匹配任何字母数字字符中的一个或多个,'?
匹配0或1'字符(用于诸如is not、dont等词),而额外的\\w*
在“字符”之后包含0个或更多的字母数字字符。可能有更好的正则表达式可供使用,但这对我很有用。检查空字符串,但不计算它?将Pattern/Matcher与word boundarisis一起使用有理由不使用s.split(“”).length
?是的,但我已经在正则表达式中插入了空格,但出于某种原因,split方法忽略了它。我知道我可以传递数组并检查它是否为空,但这是无效的。我认为可能是“,”有问题在下划线之前。您只检查s.split中的一个字符?检查空字符串,不计算空字符串?将Pattern/Matcher与单词boundarisis一起使用有一个原因您不使用s.split(“”).length
?是的,但我已经在正则表达式中插入了空格,但出于某种原因,split方法忽略了它。我知道我可以传递数组并检查它是否为空,但这是无效的。我认为可能是“,”有问题在下划线之前。您只检查s.split中的一个字符。+
是否在括号内是文字?+
是否在括号内是文字?尽管不是一个单词,不是两个。:)非常正确,虽然它是'is not'的缩写,可能是OP后面的,因为't'在所需输出中显式出现,但我可能错了:palthour不是一个单词,不是两个单词。:)非常正确,虽然它是'is not'的缩写,可能是OP后面的,因为't'在所需输出中显式出现,但我可能错了:P