Java String.split() 在元字符处 +

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16217627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:51:09  来源:igfitidea点击:

String.split() at a meta character +

javastringspecial-characters

提问by apache

I'm making a simple program that will deal with equations from a String input of the equation When I run it, however, I get an exception because of trying to replace the " +" with a " +" so i can split the string at the spaces. How should I go about using

我正在制作一个简单的程序,该程序将处理来自方程的字符串输入的方程 但是,当我运行它时,由于尝试将“+”替换为“+”,因此我可以拆分字符串,因此出现异常在空间。我应该如何使用

the string replaceAll method to replace these special characters? Below is my code

字符串 replaceAll 方法来替换这些特殊字符?下面是我的代码

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 + ^

线程“main”中的异常 java.util.regex.PatternSyntaxException:在索引 0 + 附近悬空元字符 '+'

 public static void parse(String x){
       String z = "x^2+2=2x-1";

       String[] lrside =  z.split("=",4);
       System.out.println("Left side: " + lrside[0] + " / Right Side: " + lrside[1]);
       String rightside = lrside[0];
       String leftside = lrside[1];

       rightside.replaceAll("-", " -");
       rightside.replaceAll("+", " +");
       leftside.replaceAll("-", " -"); leftside.replaceAll("+", " +");
       List<String> rightt = Arrays.asList(rightside.split(" "));
       List<String> leftt = Arrays.asList(leftside.split(" "));

       System.out.println(leftt);
       System.out.println(rightt);

采纳答案by Reimeus

replaceAllaccepts a regular expression as its first argument.

replaceAll接受一个正则表达式作为它的第一个参数。

+is a special character which denotes a quantifier meaning one or more occurrences. Therefore it should be escaped to specify the literal character +:

+是一个特殊字符,它表示一个表示一个或多个出现的量词。因此应该转义以指定文字字符+

rightside = rightside.replaceAll("\+", " +");

(Strings are immutable so it is necessary to assign the variable to the result of replaceAll);

(字符串是不可变的,因此有必要将变量分配给replaceAll);

An alternative to this is to use a character classwhich removes the metacharacter status:

对此的替代方法是使用删除元字符状态的字符类

rightside = rightside.replaceAll("[+]", " +");

The simplest solution though would be to use the replacemethod which uses non-regex Stringliterals:

最简单的解决方案是使用replace使用非正则表达式String文字的方法:

rightside = rightside.replace("+", " +"); 

回答by Subhrajyoti Majumder

String#replaceAllexpects regexas input, and +is not proper pattern, \\+would be pattern. rightside.replaceAll("\\+", " +");

String#replaceAll期望regex作为输入,而+不是正确的模式,\\+将是模式。rightside.replaceAll("\\+", " +");

回答by Aniket Thakur

I had similar problem with regex = "?". It happens for all special characters that have some meaning in a regex. So you need to have "\\"as a prefix to your regex.

我有类似的问题regex = "?"。它发生在所有在正则表达式中具有某种意义的特殊字符上。因此,您需要将其"\\"作为正则表达式的前缀。

rightside = rightside.replaceAll("\+", " +");