Reputation: 182
I looked at the existing topics, but my task is slightly different from those on the forum. I have the value:
int j = 37;
And I have one of the strings:
1) String s1 = "5+2+5+2 --j *2*7+3";
2) String s1 = "5+2+5+2 j-- *2*7+3";
3) String s1 = "5+2+5+2 ++j *2*7+3";
4) String s1 = "5+2+5+2 j++ *2*7+3";
I need with regular expression find --j
, j--
, ++j
, j++
and replace this occurrences of substring in string with number value j
;
Result string must be like this:
1) 5+2+5+2 36 *2*7+3
2) 5+2+5+2 36 *2*7+3
3) 5+2+5+2 38 *2*7+3
4) 5+2+5+2 38 *2*7+3
With pattern str.split(search).join(replacement)
I can replace the char j
with number 37
:
str.split("j").join(37);
Then I get:
1) String s1 = "5+2+5+2 --37 *2*7+3";
2) String s1 = "5+2+5+2 37-- *2*7+3";
3) String s1 = "5+2+5+2 ++37 *2*7+3";
4) String s1 = "5+2+5+2 37++ *2*7+3";
Question:
But how to perform the arithmetic operation (increment
, decrement
) at this time?
Upvotes: 0
Views: 1175
Reputation: 20185
Note that in the question, the operations do not follow the Java behaviour, i.e. pre- and postincrement both behave like preincrement.
To implement this behaviour, we need two regular expressions:
One expression matching all --j
and j--
to replace them with the value of j - 1
:
[-]{2}j|j[-]{2}
Regex101 demo
One expression matching all ++j
and j++
to replace them with the value of j + 1
:
[+]{2}j|j[+]{2}
Regex101 demo
Putting it together, we can write the following method:
public static String replaceJWith(String s, int valueForJ) {
s = s.replaceAll("[-]{2}j|j[-]{2}", Integer.toString(valueForJ - 1));
return s.replaceAll("[+]{2}j|j[+]{2}", Integer.toString(valueForJ + 1));
}
This implementation assumes that each occurrence of j
is pre- or suffixed by ++
or --
. Also, since we never really change the value of j
, using multiple pre-, and postincrement operators in a single String
will result in unexpected behaviour.
If we want to mimic the Java behaviour (i.e. distinguish between pre- and postincrement), we need three different regular expressions since we have to replace j
with three different values:
One regex to replace j(++|--)
with the original value for j
:
j(?:[+]{2}|[-]{2})
Regex101 demo
One regex to replace ++j
with the value j + 1
:
[+]{2}j
Regex101 demo
One regex to replace --j
with the value j - 1
:
[-]{2}j
Regex101 demo
Setting it all together, we can write the following Java method:
public static String replaceJWith(String s, int valueForJ) {
s = s.replaceAll("j(?:[+]{2}|[-]{2})", Integer.toString(valueForJ));
s = s.replaceAll("[+]{2}j", Integer.toString(valueForJ + 1));
return s.replaceAll("[-]{2}j", Integer.toString(valueForJ - 1));
}
This solution has the same limitations as the first solution.
Upvotes: 3
Reputation: 1160
public class Test
{
public static void main(String[] args)
{
int j = 37;
String s1 = "5+2+5+2 ++j *2*7+3";
// this line swap "j--" or "--j" on j--
s1 = s1.replaceAll("j--|--j", j-1 + "");
// this line swaps "j++" or "++j" on j++
s1 = s1.replaceAll("(j\\+\\+)|(\\+\\+j)", j+1 + "");
System.out.println(s1);
}
}
Upvotes: 5
Reputation: 19555
String.replaceAll
may be used as follows:
int j = 37;
String[] d = {
"5+2+5+2 --j *2*7+3",
"5+2+5+2 j-- *2*7+3",
"5+2+5+2 ++j *2*7+3",
"5+2+5+2 j++ *2*7+3"
};
Arrays.stream(d)
.map(s -> s.replaceAll("--j|j--", Integer.valueOf(j - 1).toString()))
.map(s -> s.replaceAll("\\+\\+j|j\\+\\+", Integer.valueOf(j + 1).toString()))
.forEach(System.out::println);
to provide expected output:
5+2+5+2 36 *2*7+3
5+2+5+2 36 *2*7+3
5+2+5+2 38 *2*7+3
5+2+5+2 38 *2*7+3
Upvotes: 3
Reputation: 2256
You can use a series of if
statements that check the string for the presence of each of those patterns using contains()
and then replace the pattern with the corresponding value:
if (s1.contains("--j") {
s1.replace("--j", String.valueOf(j-1)); //assuming you have declared int j = 37; before
} else if (s1.contains("j--")) {
//etc.
} [...]
Upvotes: 0