使用括号replaceAll方法Java [英] using parenthesis replaceAll method Java
问题描述
我想在一个句子中更改一个单词,但这个单词有括号。我把它保存在String变量中。变量 st 内部我有
I would like to change a word in a sentence, but the word has parenthesis. I keep it in the String variable. Inside of the variable st I have
manage(P0,MT0)
其他字符串变量 DES
其中包含以下句子:
Inside of other String variable DES
I have the following sentence:
查询(MT0,P0): - 不(管理(P0,MT0)),ins_manages(P0,MT0),不(医生(P0)),不(ins_physician(P0))
所以我有另一个字符串变量 querypart
,它存储了新单词:
SO I have another string variable querypart
which stores the new word:
managea
我在代码中尝试的是:
EDC = EDC.replaceAll(st,querypart);
但它不起作用。
我认为问题可能是paranthesis,所以我像st变量一样更改了字符串:
I thought the problem might be paranthesis, so I changed the string like st variable like:
manage\\(P0,MT0 \\)
然后我试过
EDC = EDC.replaceAll(st,querypart);
但我得到了一些结果。问题是当我使用replaceAll时:
but I had the some result. The problem is when I use the replaceAll like that:
EDC = EDC.replaceAll("manages\\(P0,MT0\\)",querypart);
句子更改
查询(MT0,P0): - 不(managea),ins_manages(P0,MT0),不是(医生(P0)),不是(ins_physician(P0))
我想这是因为括号但我找不到任何解决办法吗?解决方案是什么?
I guess it is because of parenthesis but I could not find any solution for that? What is the solution?
推荐答案
我相信您想使用 String#replace( )
(不是 replaceAll
),这个
I believe you wanted to use String#replace()
(not replaceAll
), this
String EDC = "query(MT0, P0) :- not(manages(P0,MT0)), "
+ "ins_manages(P0,MT0), not(physician(P0)), not(ins_physician(P0))";
String queryPart = "managesa";
EDC = EDC.replace("manages(P0,MT0)", queryPart);
System.out.println(EDC);
输出
query(MT0, P0) :- not(managesa), ins_managesa, not(physician(P0)), not(ins_physician(P0))
这篇关于使用括号replaceAll方法Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!