说明
我的目标是删除String
的特定部分。 and @ref_element='...'
。
错误
但是我遇到错误
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 97
索引97在最后一个]
之前。
代码示例
String s = "//frame[@id='tutu' and @name='toto' and @ref_element='Menu/Blabla/Toto/Titi/frame(id( abcedef']"
int length = 19;
int refElementIndex = 37;
String objectPath = "Menu/Blabla/Toto/Titi/frame(id( abcedef";
return s.replaceFirst(s.substring(refElementIndex, refElementIndex + length + objectPath.length() + 1), "");
预期结果
//frame[@id='tutu' and @name='toto']
任何主意吗?谢谢。
请勿使用replaceFirst;它使用正则表达式作为参数,而您要传递的是原始字符串,而不是正则表达式形式。只需使用.replace
;这需要原始字符串。 (注意:这将替换找到的每个“针”,而不仅仅是第一个,但是,大概不会有问题。如果必须具有replaceFirst,则将参数包装在Pattern.quote
中,这样:return s.replaceFirst(Pattern.quote(s.subst.....));
)。