0

What is the best way in Java for doing the following replacement on Strings:

I have text that looks similar to this:

one two [[**my_word** other words]] three four [[**my_other_word** other words]] five six

and I want the following text

one two **my_word** three four **my_other_word** five six

I tried using regex capture groups but how can I replace one capture group with another one?

4
  • 1
    Can you show what you tried and where you got stuck? Commented Jan 31, 2017 at 15:35
  • each group have a number $1, $2, $3 ... Then u can use it. $1 = group 1 captured, example $1 = potato => "hi $1" (hi potato). Commented Jan 31, 2017 at 15:36
  • I got stuck when I tried using the Java Pattern and Matcher API. Commented Jan 31, 2017 at 15:47
  • I added an example of how to use the Pattern and Matcher objects if you are interested in seeing how it could be done. Commented Jan 31, 2017 at 15:50

3 Answers 3

3

Use

https://www.tutorialspoint.com/java/java_string_replaceall.htm

and do something like

a.replaceAll("\\[\\[(\\w+)[^\\[]+\\]\\]", "$1");
Sign up to request clarification or add additional context in comments.

Comments

1

Depending on what your needs are, you could either use a oneliner like

a.replaceAll("\\[\\[(\\*\\*\\w+\\*\\*).*?\\]\\]", "$1");

Or the more complicated version where you can control what to replace each match with.

String inputString = "one two [[**my_word** other words]] three four [[**my_other_word** other words]] five six";
Pattern pattern = Pattern.compile("\\[\\[(\\*\\*\\w+\\*\\*).*?\\]\\]", Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputString);
StringBuffer outputBuffer = new StringBuffer();
while (matcher.find()) {
    String match = matcher.group(1);        
    matcher.appendReplacement(outputBuffer, match);
}
matcher.appendTail(outputBuffer);

String output = outputBuffer.toString();

Comments

1
a.replaceAll("\\[\\[(\\*\\*\\w+\\*\\*)(?:\\s\\w+\\s?)+\\]\\]", "$1");

5 Comments

The single backslashes you are using are not valid as regex escape characters in java. They need to be double backslashes.
Hey I just wanted to ping you on your last C++ question. You had an off by on error. std::vector<std::string> base(values->begin() + 1, values->begin() + 4); only gives you the first 3 elements. You need to change the 4 to a 5 to get the first 4 elements.
@NathanOliver ah, I knew it would be a stupid mistake. Didn't know if it was inclusive or exclusive ending bounds. Thanks!
@m_callens No problem. Generally then end is never included in the range copied.
While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem.

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.