2

I have a String

String str = (a AND b) OR (c AND d) 

I tokenise with the help of code below

String delims = "AND|OR|NOT|[!&|()]+"; // Regular expression syntax
String newstr = str.replaceAll(delims, " ");
String[] tokens = newstr.trim().split("[ ]+");

and get String[] below

[a, b, c, d]

To each element of the array I add " =1" so it becomes

[a=1, b=1, c=1, d=1]

NOW I need to replace these values to the initial string making it

(a=1 AND b=1) OR (c=1 AND d=1)

Can someone help or guide me ? The initial String str is arbitrary!

3 Answers 3

2

This answer is based on @Michael's idea (BIG +1 for him) of searching words containing only lowercase characters and adding =1 to them :)

String addstr = "=1";
String str = "(a AND b) OR (c AND d) ";

StringBuffer sb = new StringBuffer();

Pattern pattern = Pattern.compile("[a-z]+");
Matcher m = pattern.matcher(str);
while (m.find()) {
    m.appendReplacement(sb, m.group() + addstr);
}
m.appendTail(sb);
System.out.println(sb);

output

(a=1 AND b=1) OR (c=1 AND d=1)

Sign up to request clarification or add additional context in comments.

2 Comments

Please don't use a StringBuffer when you can use a StringBuilder.
@PeterLawrey Is it possible to use StringBuilder here?
2

Given:

String str = (a AND b) OR (c AND d);
String[] tokened = [a, b, c, d]
String[] edited = [a=1, b=1, c=1, d=1]

Simply:

for (int i=0; i<tokened.length; i++)
    str.replaceAll(tokened[i], edited[i]);

Edit:

String addstr = "=1";
String str = "(a AND b) OR (c AND d) ";
String delims = "AND|OR|NOT|[!&|() ]+"; // Regular expression syntax
String[] tokens = str.trim().split( delims );
String[] delimiters = str.trim().split( "[a-z]+"); //remove all lower case (these are the characters you wish to edit)

String newstr = "";
for (int i = 0; i < delimiters.length-1; i++)
    newstr += delimiters[i] + tokens[i] + addstr;
newstr += delimiters[delimiters.length-1];

OK now the explanation:

tokens = [a, b, c, d]
delimiters = [ "(" , " AND " , ") OR (" , " AND " , ") " ]

When iterating through delimiters, we take "(" + "a" + "=1".

From there we have "(a=1" += " AND " + "b" + "=1".

And on: "(a=1 AND b=1" += ") OR (" + "c" + "=1".

Again : "(a=1 AND b=1) OR (c=1" += " AND " + "d" + "=1"

Finally (outside the for loop): "(a=1 AND b=1) OR (c=1 AND d=1" += ")"

There we have: "(a=1 AND b=1) OR (c=1 AND d=1)"

12 Comments

You could also keep track of the indices where you find all the tokens, and insert the desired string:
But what in case str = "(A AND B)"? You will get str = "(A=1 A=1ND B=1)"
I can restrict str to be lower case, if there is not better solution ? how could a regex help in that case ? if u could add it to ur answer please
Let me put something together. It'll take a min.
+1 and this is little improved version of this answer. Hope you will like it.
|
1

How long is str allowed to be? If the answer is "relatively short", you could simply do a "replace all" for every element in the array. This obviously is not the most performance-friendly solution, so if performance is an issue, a different solution would be desireable.

6 Comments

I have built the Array ! I just need to put back the replaced elements of array into the initial string. Like I want to put [a=1, b=1, c=1, d=1] into string (a AND B) OR (c AND d) replacing a,b,c,d. do you get my point ?
yes, I'm saying for every element in the array, do str.replaceAll(tokens[i], tokens[i] + "=1").
@Pshemo raised a really good point "But what in case str = "(A AND B)"? You will get str = "(A=1 A=1ND B=1)"
yeah, hadn't thought of that. Well, if you still wanted to go that route, you could use regex matching to say ONLY A and not "AND" but that seems like it's getting too complicated...
Can you show it in an example or something ? It has to get complicated because as the initial string is going to be arbitrary, I need to find a generic form
|

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.