0

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

4
  • Now it's ok thanks Commented Jul 4, 2017 at 15:03
  • _ has no special meaning in regex, maybe you are confusing it with SQL's replace syntax? Commented Jul 4, 2017 at 15:04
  • 1
    @1blustone maybe he wrote anything because he says he does not what to write ^^ Commented Jul 4, 2017 at 15:05
  • @michele, after your edit the question is clear, have just provided an answer Commented Jul 4, 2017 at 15:14

1 Answer 1

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);

Java demo

Regex demo

enter image description here

Update: following your comment, if you want to extend the regex to all letters, then replace [A-E] to [A-Z].

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

2 Comments

it s not working with this expression AND(J<>Q,N-(N*10/100)>O) ...why?
@michele because you said A, B, C, D and E. If you want to replace all letters, then change A-E to A-Z

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.