Java String.matches(“(\\w)\\1”在对长度超过100的字符串进行某些执行尝试后给出不一致的结果
问题陈述:当我在代码中使用Java String.matches(“(\\w)\\1”在对长度超过100的字符串进行某些执行尝试后给出不一致的结果,java,regex,string,Java,Regex,String,问题陈述:当我在代码中使用String.matches(“(\\w)\\1”)时 在对长度超过100的字符串执行某些尝试后,它给出了不一致的结果 复制此问题的步骤: 创建java类: import java.util.Scanner; public class ReducedString { static String super_reduced_string(String s){ boolean isStringFinished = false; S
String.matches(“(\\w)\\1”)
时
在对长度超过100的字符串执行某些尝试后,它给出了不一致的结果
复制此问题的步骤:
创建java类:
import java.util.Scanner;
public class ReducedString {
static String super_reduced_string(String s){
boolean isStringFinished = false;
String newString = "";
while(!isStringFinished) {
newString = s.replaceAll("(\\w)\\1","");
s = newString;
if(!s.matches("(\\w)\\1")){
isStringFinished = true;
}
}
if(newString.length() == 0 )
newString = "Empty String";
return newString;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String result = super_reduced_string(s);
System.out.println(result);
}
}
请运行它并提供以下字符串作为输入:
OAGCIICGAOYJMAHAMJYMMWJNJWMMVPXHPPHXPVLIKAPPAKILLYYGVKKVGYMLPFDFPLMHIODVVDOIHFXPKGGPXFUEVUVVEU
基本上,代码逻辑是试图删除所有相邻的相似代码
预期输出的字符和减少的输入字符串应为:
“空字符串”。但不会得到这个结果
为了获得预期的输出,我使用方法matchPattern()
import java.util.Scanner;
公共类简化字符串{
静态字符串super\u reduced\u字符串(字符串s){
布尔值isStringFinished=false;
String newString=“空字符串”;
而(!IsString完成){
newString=s.replaceAll(“(\\w)\\1”,”;
s=新闻字符串;
如果(!匹配模式)){
isStringFinished=true;
}
}
if(newString.length()==0)
newString=“空字符串”;
返回新闻字符串;
}
静态布尔匹配模式(字符串s){
布尔值isStringFinished=false;
对于(int i=0;i
如果有人面临同样的问题,你能告诉我为什么内置函数会给出如此不一致的结果调用str.matches(regex)
相当于Pattern.matches(regex,str)
,它相当于Pattern.compile(regex).matcher(str.matches()
而Matcher.matches()
方法尝试根据模式匹配整个输入序列
见:
如评论中所述,问题在于使用String.matches()
只匹配整个字符串
此外,由于您经常使用该模式,因此在使用之前编译它将加快该过程,使其更快。我让它像这样工作:
import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class ReducedString {
static String super_reduced_string(String s){
Pattern p = Pattern.compile("(\\w)\\1"); // precompile pattern
while(s.length() > 0 && !p.matcher(s).matches()){
// check on the length to avoid matching the empty string
s = p.matcher(s).replaceAll("");
}
if(s.length() == 0 ){
s = "Empty String";
}
return s;
}
public static void main(String[] args) {
String result = super_reduced_string("oagciicgaoyjmahhamjymmwjnnjwmmvpxhpphxpvlikappakilyygvkkvgyymlpfddfplmhiodvvdoihfxpkggkpxfuevvuuvveu");
System.out.println(result);
}
}
我遵循了中给出的答案:仍然存在同样的问题。在简化字符串
中,如果(!s.matches(“(\\w)\\1”)
处于while状态,为什么不将置于while状态?您知道字符串.matches()
(实际上)会在掩码周围添加^
和$
,这是整个字符串必须匹配才能返回true
..@UsagiMiyamoto才能得到答案,所以我可以+1 itI尝试:Pattern.compile(regex).matcher(str).matches()仍然是我面临的问题。是的。请尝试Pattern.compile(regex).matcher(str).find()
。谢谢,我通过上述代码更改获得了预期结果。:),至少以给出的字符串为例。
import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class ReducedString {
static String super_reduced_string(String s){
Pattern p = Pattern.compile("(\\w)\\1"); // precompile pattern
while(s.length() > 0 && !p.matcher(s).matches()){
// check on the length to avoid matching the empty string
s = p.matcher(s).replaceAll("");
}
if(s.length() == 0 ){
s = "Empty String";
}
return s;
}
public static void main(String[] args) {
String result = super_reduced_string("oagciicgaoyjmahhamjymmwjnnjwmmvpxhpphxpvlikappakilyygvkkvgyymlpfddfplmhiodvvdoihfxpkggkpxfuevvuuvveu");
System.out.println(result);
}
}