Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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_String_Algorithm - Fatal编程技术网

如何将Java正则表达式匹配放入结果字符串?

如何将Java正则表达式匹配放入结果字符串?,java,regex,string,algorithm,Java,Regex,String,Algorithm,请参考上述问题。我从未使用过java正则表达式。如何将所有子字符串放入带有匹配字符(如“(“”“.””等符号)的新字符串中,并用单个空格分隔。例如,在正则表达式之前 String c= "List<String> uncleanList = Arrays.asList(input1.split("x"));" ; String c=“List uncleanList=Arrays.asList(input1.split(“x”));" ; 我想要这样的结果字符串 String

请参考上述问题。我从未使用过java正则表达式。如何将所有子字符串放入带有匹配字符(如“(“”“.””等符号)的新字符串中,并用单个空格分隔。例如,在正则表达式之前

 String c= "List<String> uncleanList = Arrays.asList(input1.split("x"));" ;
String c=“List uncleanList=Arrays.asList(input1.split(“x”));" ;
我想要这样的结果字符串

String r= " List < String > uncleanList = Arrays . asList ( input1 . split ( " x " ) ) ; "
String r=“ListuncleanList=数组。asList(输入1.拆分(“x”));"

引用链接到的代码,
matcher.group()
将为您提供一个标记。只需使用StringBuilder附加此标记和一个空格,即可获得一个新字符串,其中标记是空格分隔的

String c = "List<String> uncleanList = Arrays.asList(input1.split(\"x\"));" ;
Pattern pattern = Pattern.compile("\\w+|[+-]?[0-9\\._Ee]+|\\S");
Matcher matcher = pattern.matcher(c);

StringBuilder sb = new StringBuilder();
while (matcher.find()) {
    String token = matcher.group();
    sb.append(token).append(" ");
}
String r = sb.toString();
System.out.println(r);

String c=“List uncleanList=Arrays.asList(input1.split(\“x\”);" ;
Pattern Pattern=Pattern.compile(\\w+.[+-]?[0-9\\.\u Ee]+.\\S”);
Matcher-Matcher=pattern.Matcher(c);
StringBuilder sb=新的StringBuilder();
while(matcher.find()){
字符串标记=matcher.group();
某人追加(代币)。追加(“”);
}
字符串r=sb.toString();
系统输出println(r);
String c=“List uncleanList=Arrays.asList(input1.split('x'));";
Matcher Matcher=Pattern.compile(“\\\\\\”)(“\\\\”)”).Matcher(c);
while(matcher.find()){
String symbol=matcher.group();
c=c.替换(符号“+”符号+”);
}

实际上,如果你仔细观察,你会发现你只需要分开字母符号和空格((?![a-zA-Z]|\)

输入字符串包含“x”而不是“x”。你检查过它是如何工作的吗?它工作得很好,我写的“因为我不想写\”代码工作得很好…但我的后续问题是:如果我需要忽略“”或“”或[]之间的符号。那么我该怎么做呢?请根据我的问题回答并参考一些有用的教程。这基本上是
String r=pattern.matcher(c).replaceAll的手动版本(“$0”);
您甚至可以临时进行替换,直接在输入字符串上调用
replaceAll
,如
System.out.println(c.replaceAll(\\w+.[+-]?[0-9\\.\u Ee]+\\\S,“$0”);
    String c = "List<String> uncleanList = Arrays.asList(input1.split('x'));";
    Matcher matcher = Pattern.compile("\\<|\\>|\\\"|\\.|\\(|\\)").matcher(c);

    while(matcher.find()){
        String symbol = matcher.group();
        c = c.replace(symbol," " + symbol + " ");
    }