0

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

manages(P0,MT0)

Inside of other String variable DES I have the following sentence:

query(MT0, P0) :- not(manages(P0,MT0)), ins_manages(P0,MT0), not(physician(P0)), not(ins_physician(P0))

SO I have another string variable querypart which stores the new word:

managesa

What I tried in the code was:

EDC = EDC.replaceAll(st,querypart);

but it did not work.

I thought the problem might be paranthesis, so I changed the string like st variable like:

manages\\(P0,MT0\\)

then I tried

EDC = EDC.replaceAll(st,querypart);

but I had the some result. The problem is when I use the replaceAll like that:

EDC = EDC.replaceAll("manages\\(P0,MT0\\)",querypart);

The sentence changes

query(MT0, P0) :- not(managesa), ins_manages(P0,MT0), not(physician(P0)), not(ins_physician(P0))

I guess it is because of parenthesis but I could not find any solution for that? What is the solution?

BitNinja
  • 1,477
  • 1
  • 19
  • 25
nic
  • 125
  • 1
  • 3
  • 7
  • Why you won't to use regular expressions? – Ivan Aug 19 '14 at 00:30
  • Possible duplicate: [link](http://stackoverflow.com/questions/1138552/replace-string-in-parentheses-using-regex) – Sherz Aug 19 '14 at 00:32
  • 1
    What do you want the result to be? It looks like the only part of your "EDC" string that changed is the part that you wanted to change: `manages(P0,MT0)` was replaced with `managesa`. Is that not the result you wanted? – Edward Aug 19 '14 at 01:05

1 Answers1

1

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);

Output is

query(MT0, P0) :- not(managesa), ins_managesa, not(physician(P0)), not(ins_physician(P0))
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • not exactly what I use is variables and I don't fill by myself so EDC replace should be EDC = EDC.replace(st, queryPart); but in this case I can't get any change. – nic Aug 19 '14 at 00:40
  • Read my code again please. I change the first and the second `manages(P0,MT0)`. What do you want the output to be? – Elliott Frisch Aug 19 '14 at 00:42