1

I think I have a decent handle wrt matching strings using Regex in Java, but now I am trying to replace strings using Regex and not having much success.

Simply put, I am trying to find where there is a digit immediately followed by a constant string "CMR", then adding a space between the digit and the "CMR" substring. "0CMR" should become "0 CMR", "5CMR" should become "5 CMR", etc. Any preceding non-digit should be left as it was.

So my source string is "theStringThat0CMRhas"

my command is:

replaceAll("[0-9]CMR", "[0-9] CMR");

I get the added space in the result, but the result becomes "theStringThat[0-9] CMRhas" which obviously isn't what I need. Somehow I need to tell Regex not to replace with "[0-9]", but with whatever it matched on in the first place.

I know I'm doing this wrong, but I don't know what's right.

Any help appreciated.

Thanks,

Tom

1

4 Answers 4

10

You want to use a capturing group:

replaceAll("([0-9])CMR", "$1 CMR")

$1 references the first group in the match, denoted by parentheses.

Also, [0-9] can be substituted with \d.

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

2 Comments

Can you add some description and instructions explain how this works? As a Java beginner won't understand what are you typing about.
Perfect answer, @shmosel. Not only showed the solution, but included the name and url of the description where I can go for more in-depth knowledge of the subject. Thanks muchly!
1

Try this:

replaceAll("(?<=\\d)(?=\\D)"," ")

It uses look ahead for non digit character and negative look ahead for digit characters.

If you want just do it for the one with CMR after the digits, use:

"(?<=\\d)(?=CMR)"

Comments

0

You should group the number regex and call argument. Your code here:

replaceAll("([0-9])CMR", "$1 CMR");

For more regex knowledge, please read this document https://www.tutorialspoint.com/java/java_regular_expressions.htm

Good luck!

Comments

0

a good starting point may be here for reading regex: http://www.regular-expressions.info/java.html

on this site the replacing string page is here: http://www.regular-expressions.info/replacetutorial.html

$with a number represents a whole regex match, and you can use these to refer to what you were doing

    String testString = "theStringThat0CMRhas";
    String resultString = testString.replaceAll("[0-9]CMR","$0");
    System.out.println(resultString);

this would result in the answer: theStringThat0CMR has

you obviously didnt want this, so lets change the answer up a little

    String testString = "theStringThat0CMRhas";
    String resultString = testString.replaceAll("([0-9])CMR","$0 CMR");
    System.out.println(resultString);

now we are referencing the parenthsis, in which it hasn't done anything yet, so its replacing what it found, with the same thing, a space, and CMR

your result would now be: theStringThat0CMR CMRhas

so lets reference the part where we have chosen the number

    String testString = "theStringThat0CMRhas";
    String resultString = testString.replaceAll("([0-9])CMR","$1 CMR");
    System.out.println(resultString);

now your answer will be: theStringThat0 CMRhas

it is finding where it picked a number, replacing it with that number, a space, and then CMR

you are trying to do what I believe to be called a backreference though I am unsure. Regex is still not my strong suit either.

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.