2

Need help in getting the java regex to replace = sing between parenthesis with #, my input text is

8=FIX.4.&49=(550=0449)&35=RIO&76=(AB=4560)&

expected output string

8=FIX.4.&49=(550#0449)&35=RIO&76=(AB#4560)&

So would like to replace = char only within (550=0449) and (AB=4560) with # so the output should contain (550#0449) and (AB#4560).

2
  • is it possible to have orphaned or nested "(" and ")" or will they always have an open and close perenthesis? Commented Feb 5, 2015 at 22:40
  • It can be nested but it will be balanced, every opening ( will have closing ) so every = char within ( needs to be replaced even from nested. Commented Feb 5, 2015 at 22:46

2 Answers 2

3

I like anubhava's answer, but if you want to be more strict and assert there are non-blank terms and opening and closing brackets, capture the terms and write them back using back references:

str = str.replaceAll("(\\(\\w+)=(\\w+\\))", "$1#$2");
Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

String repl = str.replaceAll("=(?=[^()]*\\))", "#");

(?=[^()]*\)) is a lookahead that will make sure to match = only when there is a ) following it.

Comments

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.