package files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
\t public static String arr[];
\t public static void main(String[] args) throws Exception {
\t \t
\t \t String fiveFour="kkkkaaaaa";
\t \t String condition="[a]{5}|[k]{1}";
\t \t int size=0;
\t \t regTest(condition,fiveFour,size);
\t System.out.println(size);
\t \t
\t \t
\t \t
\t }
\t
\t public static int regTest(String regexThing, String toCheck, int size){
\t \t Pattern pattern= Pattern.compile(regexThing);
\t \t Matcher regexMatcher= pattern.matcher(toCheck);
\t \t
\t \t int i=0;
\t \t
\t \t while(regexMatcher.find()){
\t \t \t i++;
\t \t \t System.out.println(regexMatcher.group());
\t \t \t if(regexMatcher.group().length()!=0){
\t \t \t \t System.out.println(regexMatcher.group().length());
\t \t \t \t
\t \t \t \t
\t \t \t \t
\t \t \t }
\t \t \t
\t \t \t
\t \t }
\t \t
\t \t return i;
\t \t
\t }
\t // work on return try maybe a increment and returning that at the end!
\t //stacks
\t
}
我無法回到我regexMatcher.group().length()
,在我的心意它打印出5這是正確的。但是它不會將該值分配給大小,因此我可以將其返回。任何幫助?這是一個邏輯錯誤,我沒有看到我出錯的地方。
package files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static String arr[];
public static void main(String[] args) throws Exception {
String fiveFour="kkkkaaaaa";
String condition="[a]{5}|[k]{1}";
int size=0;
regTest(condition,fiveFour,size);
System.out.println(size);
}
public static int regTest(String regexThing, String toCheck, int size){
Pattern pattern= Pattern.compile(regexThing);
Matcher regexMatcher= pattern.matcher(toCheck);
int i=0;
while(regexMatcher.find()){
i++;
System.out.println(regexMatcher.group());
if(regexMatcher.group().length()!=0){
System.out.println(regexMatcher.group().length());
i++;
}
}
return size;
}
// work on return try maybe a increment and returning that at the end!
//stacks
}
hrmmm ...任何建議?即時通過方法尋找 –