Home > Mobile >  How to delete the second number in the pair of numbers
How to delete the second number in the pair of numbers

Time:05-29

I have a file that contains list of games with its price, like:

    Lego Star Wars: The Skywalker Saga  & 
     479,20 599
    
    
    Kena: Bridge Of Spirits  & 
     246,35 379


    Red Dead Redemption 2
    187,60 469

The number on the left is the price after discount, the number on the right is the price before the discount. I need to read the file and delete all the numbers on the right. How can I do that? I've tried to use replace.all() method but it didn't work.

The result must be:

         Lego Star Wars: The Skywalker Saga  & 
         479,20
        
        
        Kena: Bridge Of Spirits  & 
         246,35 
    
    
        Red Dead Redemption 2
        187,60

CodePudding user response:

You could use a regex replacement, e.g.

String input = "Lego Star Wars: The Skywalker Saga  & \n     479,20 599";
String output = input.replaceAll("(\\d (?:,\\d )*) \\d ", "$1");
System.out.println(output);

This prints:

    Lego Star Wars: The Skywalker Saga  & 
     479,20
  • Related