Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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,嗨,我开始学习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");

嗨,我开始学习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");
        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)
检查字符串三次