使用replaceAll在字符串中出现重复时的非法重复错误。

huangapple go评论56阅读模式
英文:

Illegal repetition near index error when using replaceAll in string

问题

I want to remove the divPrefix string from request string, but I am getting "illegal repetition near index 142" error message. Could you please help to resolve this issue.

Note: This divPrefix is in the json request, so it will be there in multiple places. Is there a change to replace it in one shot?

String request="<div class="artemis-editor" style="font-family:Arial,sans-serif!important;font-size: 11px!important;">

Paragraph content

";

String divPrefix="<div class="artemis-editor" style="font-family:Arial,sans-serif!important;font-size: 11px!important;">

";

request=request.replaceAll(divPrefix,"");
request=request.replaceAll("

","");

Expected output:

Paragraph content

英文:

I want to remove the divPrefix string from request string, but I am getting "illegal repetition near index 142" error message. Could you please help to resolve this issue.

Note : This divPrefix is in the json request, so it will be there in multiple places. Is there a change to replace it in one shot?

String request=&quot;&lt;div class=&quot;artemis-editor&quot; style=&quot;font-family:Arial,sans-serif!important;font-size: 11px!important;&quot;&gt;&lt;style&gt;.artemis-editor,.artemis-editor * {font-family:Arial,sans-serif!important;font-size: 11px!important;}&lt;/style&gt;&lt;p&gt;Paragraph content&lt;/p&gt;&lt;/div&gt;&quot;;

String divPrefix=&quot;&lt;div class=&quot;artemis-editor&quot; style=&quot;font-family:Arial,sans-serif!important;font-size: 11px!important;&quot;&gt;&lt;style&gt;.artemis-editor,.artemis-editor * {font-family:Arial,sans-serif!important;font-size: 11px!important;}&lt;/style&gt;&quot;;

request=request.replaceAll(divPrefix,&quot;&quot;);
request=request.replaceAll(&quot;&lt;/div&gt;&quot;,&quot;&quot;);

Expected output:
&lt;p&gt;Paragraph content&lt;/p&gt;

答案1

得分: 4

replaceAll方法的第一个参数应该是一个正则表达式。如果你只想匹配一个普通的子字符串,可以使用String.replace代替。

英文:

The replaceAll method takes a regex as the first argument. You just want it to match a regular substring. For that, use String.replace instead.

答案2

得分: 1

第一个参数传递给 replaceAll 必须是一个正确格式的正则表达式,而不是随便的字符字符串。你的复杂的HTML片段并不是一个正则表达式。

也许更简单的方法是通过使用 indexOf 来定位你想要移除的字符串,然后使用 substring 来移除它。

关于String的文档

另外,虽然我没有尝试过这个方法,但可以考虑将整个 'divPrefix' 字符串包装在 \Q\E 中。

关于Pattern的文档

个人而言,我不太喜欢这种方法;我觉得正则表达式有点被滥用了,在这种情况下,我们正在使用正则表达式,但实际上我们并不想要一个正则表达式,所以我们正在找一种方法让它被视为 '不是真正的正则表达式'。

英文:

The first argument to replaceAll has to be a correctly-formed regular expression, not any old character string. Your complex piece of HTML is not a regular expression.

It might be simpler to handle this 'manually' by using indexOf to locate the string you want to remove, and then substring to remove it.

Documentation for String.

Alternatively, and I have not tried this, consider wrapping the entire 'divPrefix' string in \Q and \E.

Documentation for Pattern.

Personally, I don't like that approach; I find regular expressions to be somewhat overused, and in this case we are using a regular expression where we don't actually want a regular expression, so we're finding a way to make it be treated as 'not really a regular expression'.

答案3

得分: 1

当您使用replaceAll()函数时,您需要将正则表达式作为第一个参数传递。为了实现您的目的,您需要使用replace()函数。修改后的代码:

request = request.replace(divPrefix, "");
request = request.replace("</div>", "");
英文:

When you use the the replaceAll() function, you need to pass a regex(regular expression) as the first argument. For achieving your purpose, you need to use the replace() function. Modified code:

request=request.replace(divPrefix,&quot;&quot;);
request=request.replace(&quot;&lt;/div&gt;&quot;,&quot;&quot;);

答案4

得分: 1

使用 String#replace 方法,而不是 String#replaceAll 方法。

后者会将您的参数解释为 正则表达式模式

String request = "&lt;p&gt;段落内容&lt;/p&gt;";
String divPrefix = "&lt;div class=\"artemis-editor\" style=\"font-family:Arial,sans-serif!important;font-size: 11px!important;\"&gt;&lt;style&gt;.artemis-editor,.artemis-editor * {font-family:Arial,sans-serif!important;font-size: 11px!important;}&lt;/style&gt;";
request=request.replace(divPrefix,"");
request=request.replace("&lt;/div&gt;","");
System.out.println(request);

输出

&lt;p&gt;段落内容&lt;/p&gt;
英文:

Use the String#replace method, and not the String#replaceAll method.

The latter will interpret your argument as a regular expression pattern.

String request = &quot;&lt;div class=\&quot;artemis-editor\&quot; style=\&quot;font-family:Arial,sans-serif!important;font-size: 11px!important;\&quot;&gt;&lt;style&gt;.artemis-editor,.artemis-editor * {font-family:Arial,sans-serif!important;font-size: 11px!important;}&lt;/style&gt;&lt;p&gt;Paragraph content&lt;/p&gt;&lt;/div&gt;&quot;;
String divPrefix = &quot;&lt;div class=\&quot;artemis-editor\&quot; style=\&quot;font-family:Arial,sans-serif!important;font-size: 11px!important;\&quot;&gt;&lt;style&gt;.artemis-editor,.artemis-editor * {font-family:Arial,sans-serif!important;font-size: 11px!important;}&lt;/style&gt;&quot;;
request=request.replace(divPrefix,&quot;&quot;);
request=request.replace(&quot;&lt;/div&gt;&quot;,&quot;&quot;);

System.out.println(request);

Output

&lt;p&gt;Paragraph content&lt;/p&gt;