Java 如何在字符串中查找多个特定子字符串
嗨,我开始学习java,但是我对正则表达式有一个问题,我有一个这样的字符串 str=“intf(inta,intb)” 我想知道发生了多少个“int” 我读了oracle的文档,发现我们有一个名为X{n}的正则表达式,它告诉我们,例如,“int”是否正好匹配n次 我写了这段代码,但它给了我错误的答案Java 如何在字符串中查找多个特定子字符串,java,regex,Java,Regex,嗨,我开始学习java,但是我对正则表达式有一个问题,我有一个这样的字符串 str=“intf(inta,intb)” 我想知道发生了多少个“int” 我读了oracle的文档,发现我们有一个名为X{n}的正则表达式,它告诉我们,例如,“int”是否正好匹配n次 我写了这段代码,但它给了我错误的答案 String temp = "int f ( int a , int b )"; Pattern pattern = Pattern.compile("\\bint{3}\\b");
String temp = "int f ( int a , int b )";
Pattern pattern = Pattern.compile("\\bint{3}\\b");
Matcher matcher = pattern.matcher(temp);
if(matcher.find()
system.out.print("found");
任何想法您可以在匹配器上反复调用find()
,以查找模式的重复副本。因此,搜索/\bint\b/
,然后计算find()
返回true
的次数,您可以在匹配器上重复调用find()
,以查找模式的重复副本。因此,搜索/\bint\b/
,然后计算find()
返回true
的次数,您可以在匹配器上重复调用find()
,以查找模式的重复副本。因此,搜索/\bint\b/
,然后计算find()
返回true
的次数,您可以在匹配器上重复调用find()
,以查找模式的重复副本。因此,搜索/\bint\b/
,然后计算有多少次find()
返回true
int executions=0;
int occurrences = 0;
int index = 0;
while (index < str.length() && (index = str.indexOf("int", index)) >= 0) {
occurrences++;
index += 3; //'length' of (characters in) 'int'
}
int指数=0;
而(index=0){
事件++;
索引+=3;//“int”中(字符)的“长度”
}
出现次数
保存您要查找的数字(即“int”的次数可在str
中找到)。int出现次数=0;
int指数=0;
而(index=0){
事件++;
索引+=3;//“int”中(字符)的“长度”
}
出现次数
保存您要查找的数字(即“int”的次数可在str
中找到)。int出现次数=0;
int指数=0;
而(index=0){
事件++;
索引+=3;//“int”中(字符)的“长度”
}
出现次数
保存您要查找的数字(即“int”的次数可在str
中找到)。int出现次数=0;
int指数=0;
而(index=0){
事件++;
索引+=3;//“int”中(字符)的“长度”
}
executions
保存您要查找的数字(即,可以在str
中找到“int”的次数)。您误解了X{N}
:它匹配上一表达式的后续事件,因此a{3}
将匹配aaa
,而不是abababa
此任务不应使用正则表达式。它们只是在计算事件时效率低下
int countOccurrences(String str, String what) {
int index, n;
index = n = 0;
// Increase n as long as there are occurrences after the
// last found index
while(index < str.length() && (index = str.indexOf(what, index)) != -1) {
n++;
index += what.length();
}
return n;
}
int countoccurrencess(字符串str,字符串what){
int指数,n;
指数=n=0;
//增加n,只要在
//最后找到的索引
而(index
您误解了X{N}
:它匹配前面表达式的后续出现,因此a{3}
将匹配aaa
,而不是ababa
此任务不应使用正则表达式。它们只是在计算事件时效率低下
int countOccurrences(String str, String what) {
int index, n;
index = n = 0;
// Increase n as long as there are occurrences after the
// last found index
while(index < str.length() && (index = str.indexOf(what, index)) != -1) {
n++;
index += what.length();
}
return n;
}
int countoccurrencess(字符串str,字符串what){
int指数,n;
指数=n=0;
//增加n,只要在
//最后找到的索引
而(index
您误解了X{N}
:它匹配前面表达式的后续出现,因此a{3}
将匹配aaa
,而不是ababa
此任务不应使用正则表达式。它们只是在计算事件时效率低下
int countOccurrences(String str, String what) {
int index, n;
index = n = 0;
// Increase n as long as there are occurrences after the
// last found index
while(index < str.length() && (index = str.indexOf(what, index)) != -1) {
n++;
index += what.length();
}
return n;
}
int countoccurrencess(字符串str,字符串what){
int指数,n;
指数=n=0;
//增加n,只要在
//最后找到的索引
而(index
您误解了X{N}
:它匹配前面表达式的后续出现,因此a{3}
将匹配aaa
,而不是ababa
此任务不应使用正则表达式。它们只是在计算事件时效率低下
int countOccurrences(String str, String what) {
int index, n;
index = n = 0;
// Increase n as long as there are occurrences after the
// last found index
while(index < str.length() && (index = str.indexOf(what, index)) != -1) {
n++;
index += what.length();
}
return n;
}
int countoccurrencess(字符串str,字符串what){
int指数,n;
指数=n=0;
//增加n,只要在
//最后找到的索引
而(index
为什么要使用复杂的正则表达式?您可以使用String#indexOf(int,int)
连续三次检查字符串-正则表达式可以使用NFA匹配表达式,如果您没有非确定性,我认为正则表达式很少使用。为什么要使用复杂的正则表达式?您可以使用String#indexOf(int,int)
连续三次检查字符串-正则表达式可以使用NFA匹配表达式,如果您没有非确定性,我认为正则表达式很少使用。为什么要使用复杂的正则表达式?您可以使用String#indexOf(int,int)
连续三次检查字符串-正则表达式可以使用NFA匹配表达式,如果您没有非确定性,我认为正则表达式很少使用。为什么要使用复杂的正则表达式?您可以使用String#indexOf(int,int)
检查字符串三次