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?

3
  • Why you won't to use regular expressions? Commented Aug 19, 2014 at 0:30
  • Possible duplicate: link Commented Aug 19, 2014 at 0: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? Commented Aug 19, 2014 at 1:05

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Read my code again please. I change the first and the second manages(P0,MT0). What do you want the output to be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.