I want to replace a string in Java, but there are the following rules.
Do not replace abc.
Do not replace desk.
Replace a with x.
Replace c with x.
Replace d with x.
String str = "abcdefg desktop ac";
String replaced = "abcxefg desktop xx";<- str I want to replace like this
If i simply replace acd, you will also replace ac in abc and d in desk.
I think the following logic will work,
The string that should not be replaced is variable, and the end of the string must be determined.
I think the process becomes a little complicated.
for (int i = 0;i<str.length;i++) {
// If str.substring(i, 3) is not abc and str.substring(i, 4) is not desk
// If str.substring(i, 1) is a, c, d, replace
}
Is there any good regular expression or logic?
-
Answer # 1
-
Answer # 2
public class Test1 { public static void main(String[] args) { String str1 = "abcdefg desktop ac"; String array1[] = str1.split("(?<=(abc|desk))|(?=(abc|desk))"); ...
Disassemble into an array like this and execute replacement only when each element is not abc or desk. How about putting them all together and turning them back into a single string?
-
Answer # 3
If you think that "a without bc following", "c without ab before" or "d without esk after" is replaced with x,Negative lookahead, negative lookbehindIt is possible by using.
String str = "abcdefg desktop ac"; String replaced = str.replaceAll("(a(?!bc)|c(?<!abc)|d(?!esk))", "x");
Related articles
- java - please tell me how to put the matrix in string [] [] into matrix
- convert integer number to string year/month display in java
- java - i want to pass a string to another thread
- [java] i want to take the value of the character string with the largest number of characters
- java - how to make string[] from int[] *for prohibited
- java - about string conversion using the methods of the string class
- i want to handle backslashes in string replacement in python
- convert double to string in java
- java - [android] i want to get colors as a character string from colorsxml change the color of vector image "setcolorfilter
- java - the content of the string type variable becomes null
- i want to convert a string into a mathematical expression in java
- i want to put a character string (row unit) that i do not know whether the encoding is utf-8 or sjis in java in list
- java - send data in json format but cannot receive it as a json string
- i want to detect an empty string with java regular expression
- java - i want to make date containing 3 digits of string of 2 digits after the decimal point
- java - i want to perform a conditional search and display the results
- i want to convert a string to a number with java servlet
- validate the character string received by java scanner one by one, and prompt for re-entry if invalid character is included
- java - i want to be able to declare ○○○○(string title){
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
For the time being, I think it's good to implement the rules you wrote.
1st stageIf you do it, probably do this.
(1) Do not replace abc.
(2) Do not replace desk.
First, replace it with another character in the regular expression.
Second stageDepending on what you are replacing, let's say (1) [email protected] and (2) desk=#.
In reality, you should replace it with a character that you can't type.
Since abc and desk are already different characters, we simply replace them with regular expressions using the following rules.
(3) Replace a with x.
3rd stage(4) Replace c with x.
(5) Replace d with x.
Replace the one that was replaced in the first step.
@ =>abc, # =>desk