Replace number with words in correct position in java

In fact, I'm trying to replace the number with words in a sentence that the user gives. The date format of this event; For example: My birthday is on 16/6/2000 and I'm newbie to the javabecome --->My birthday is on sixteenth july two thousand and I'm newbie to the java

Here is the code:

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter any numbers: ");
        String nom = reader.nextLine(); // get input from user

        //checking contains that has "/" or not
        if(nom.contains("/")){
           String parts[] = nom.split("[/]"); 
           String part1 = parts[0]; //data before "/" will be stored in the first array
           String day[] = part1.split("\\s+");// split between space
           String get_day = day[day.length -1];// get last array

           String get_month = parts[1]; //data in the between of "/" will be stored in the second array

           String part3 = parts[2]; // data after "/" will be stored in the third array
           String year[] = part3.split("\\s+");// split between space
           String get_year = year[0];// get first array

           String s = NumberConvert.convert(Integer.parseInt(get_day)) + 
                      NumberConvert.convert(Integer.parseInt(get_month)) + 
                      NumberConvert.convert(Integer.parseInt(get_year));

           String con = nom.replaceAll("[0-9].*/[0-9].*/[0-].*", s); // replace number to word        
           System.out.println(con); // print the data already converted
        } else {....}

But I got the result:

My birthday is on sixteenth july two thousand 

//"and I'm newbie to the java" is disappear [How to solve it]//

How to solve it. In fact, I want to get the value before and after the "/" slash and convert it to words and replace it as the user's original input.

I tried:

String con = nom.replaceAll("[0-9].*/[0-9].*/[0-9999]", s); // a bit change [0-9].* to [0-9999] 

But the result will look like this:

My birthday is on sixteenth july two thousand 000 and I'm newbie to the java
//after two thousand index "000" is appearing
+4
source share
2 answers

The regular expression is wrong:

[0-9].*/[0-9].*/[0-].*

What does it mean:

[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-] match a single number in the list 0- literally
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]

It should be:

[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]

Or better:

\d{2}/\d{2}/\d{4}
+3
source

You can also use the regex template below to get all numbers from a string:

        String st = "My birthday is on 16/6/2000 and I'm newbie to the java, using since 2015";     
        Pattern p = Pattern.compile("-?\\d+");
        Matcher m = p.matcher(st);
        while (m.find()) {
          System.out.println(m.group());
        }
0

All Articles