What is the regex expression for replace all the A char with A1, B->B1, C->C1, D->D1 and E->E1 string?
//AND(A<>B,C>D)?GREEN(E-E)
String expr ="AND(A<>B,C>D)?GREEN(E-E)";
String regex="";
expr.replaceAll(regex, "N1");
System.out.println(expr);
The result may be:
AND(A1<>B1,C1>D1)?GREEN(E1-E1)
Thank you
asked Jul 4, 2017 at 15:00
michele
26.7k31 gold badges119 silver badges173 bronze badges
1 Answer 1
You can use a regex like this:
\b([A-E])\b
With the replacement string 11ドル
Bear in mind that in java you have to escape backslasher, so you have to use:
String expr = "AND(A<>B,C>D)?GREEN(E-E)";
expr = expr.replaceAll("\\b([A-E])\\b", "11ドル");
System.out.println(expr);
Update: following your comment, if you want to extend the regex to all letters, then replace [A-E] to [A-Z].
answered Jul 4, 2017 at 15:04
Federico Piazza
31.2k15 gold badges91 silver badges133 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
michele
it s not working with this expression AND(J<>Q,N-(N*10/100)>O) ...why?
Federico Piazza
@michele because you said A, B, C, D and E. If you want to replace all letters, then change
A-E to A-Zlang-java
_has no special meaning in regex, maybe you are confusing it with SQL's replace syntax?