0

The task at hand is to replace "-" with "/" in a birthday format e.g. 03-12-89 -> 03/12/89. However, the "-" must be able to appear elsewhere in the string e.g. "My-birthday-is-on-the: 03/12/89".

I have tried creating substrings, replace the "-" in the birthday part and then combine the strings again. However, that solution is inflexible and fails the testcases.

I'm thinking I must be able to do this with a regular expression, although I seem unable to construct it. So now I'm back to: String newStr = input.replace("-", "/"); Which remove all instances of "-" which I don't want.

Can anyone help?

3
  • 1
    Use a regular expression to find the birthday, then only replace the characters within that match range Commented Jun 14, 2018 at 5:50
  • @MadProgrammer that's what I want, but how? Commented Jun 14, 2018 at 5:51
  • You can parse dates to find they are dates, and then apply regex only to them Commented Jun 14, 2018 at 5:51

2 Answers 2

3

You can use the following regex:

(?<=\d{2})-

with replacement \/ (no need to escape it in Java)

INPUT:

My-birthday-is-on-the: 03-12-89

OUTPUT:

My-birthday-is-on-the: 03/12/89

demo

Code:

String input = "My-birthday-is-on-the: 03-12-89";
System.out.println(input.replaceAll("(?<=\\d{2})-", "/"));

OUTPUT:

My-birthday-is-on-the: 03/12/89
Sign up to request clarification or add additional context in comments.

3 Comments

@TimBiegeleisen: You are right for the escape part thanks ;-)
It works the way you made it, but inside my method it doesn't? Any idea why? ´private static String changeFormat(String input) { String newStr = input.replace("(?<=\\d{2})-", "/"); return newStr; }´ newStr is the same as the input string
@Kenneth: this is normal the method should be replaceAll and not replace as described in my answer
2

The easiest way which comes to mind is just match \d{2}-\d{2}-\d{2}, with capture groups. Then, use those captured numbers to rebuild the birthdate the way you want it. Something like this:

String input = "My-birthday-is-on-the: 03/12/89";
input = input.replaceAll("\\b(\\d{2})-(\\d{2})-(\\d{2})\\b", "$1/$2/$3");

Demo

The advantage of specifying the full pattern is that it avoids the chance of matching anything other than a 6 digit dash-separated birthday.

Edit:

Based on your comment below, it sounds like maybe you want to do this replacement on a two dash separated number, with any number of digits. In this case, we can slightly modify the above code to the following:

String input = "Your policy number is: 123-45-6789.";
input = input.replaceAll("\\b(\\d+)-(\\d+)-(\\d+)\\b", "$1/$2/$3");

4 Comments

Inside the my method: private static String changeFormat(String input) { String newStr = input.replaceAll("\\b(\\d{2})-(\\d{2})-(\\d{2})\\b", "$1/$2/$3"); return newStr; } The output is still the same as the input. I don't get it (i have accounted for the wrong input in your example.)
@Kenneth I don't know your code. Try to start with my demo, or the other answer, and go from there.
Code is simple: call this method: System.out.println(changeFormat("Your policy number is: 123-45-6789.")); With the above implementation
I love you :) Thank you SO much. Code works. I'll analyse to see why and learn something from it.

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.