0

I have some String like this:

"this is a string like #{aa} and#{bb}. "
"#{cc}this is another str#{dd}ing..."

I want to change these String like this:

"this is a string like ? and?." "aa" "bb"
"?this is another str?ing..." "cc" "dd"

I tried to use regular expressions to split these string and failed.

What should I do?

2 Answers 2

1

You can replace the string using a regex like this:

#\{(.*?)\}

Working demo

Then you have to grab the content from the capturing group and concatenate it to your string. I leave the logic for you :)

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

Comments

0

You can try like this:

final String regex = "#\\{(.*?)\\}";
final String string = "ere is some string #{sx} and #{sy}.";
final String subst = "?";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.print(matcher.group(1) + " ");
}

final String result = matcher.replaceAll(subst);
System.out.println(result);

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.