3

I have a string that contains multiple numbers which have to be processed and replaced in the same string again.

For example:

let's say I have:

my name is anusha, I am a noob in Java having reputation: 3647 haha I am just kidding my actual reputation is 0001

Now lets say I'd like to extract 3647 and multiply or divide or add something to it. Let's divide 3647/100 = 36.47 and replace with the original number in string same for 0001 and replace by 00.01.

Result String should be:

my name is anusha, I am a noob in Java having reputation: 36.47 haha I am just kidding my actual reputation is 00.01

Appreciate your help. I know this is silly for many but I'm still learning.

I tried doing:

Pattern intsOnly = Pattern.compile("\\d+");
    Matcher makeMatch = intsOnly.matcher("my name is anusha, I am a noob in Java having reputation: 3647 haha I am just kidding my actual reputation is 0001");
    makeMatch.find();
    String inputInt = makeMatch.group();
    System.out.println(inputInt);

But obviously it only picks up the first number because i did not use a loop, also I'm not really sure how to process the number.

Anusha
  • 204
  • 1
  • 4
  • 12

4 Answers4

2

Try this:

    final String regex = "\\d+";
    String string = "my name is anusha, I am a noob in Java having reputation: 3647 haha I am just kidding my actual reputation is 0001";
    NumberFormat formatter = new DecimalFormat("00.00");     

    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(string);

    while (matcher.find()) {
        Float val=new Float(matcher.group(0));
        val=val/100;
        string=string.replace(matcher.group(0),formatter.format(val));
    }
    System.out.println(string);

}

Output:

my name is anusha, I am a noob in Java having reputation: 36.47 haha I am just kidding my actual reputation is 00.01
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • thanks for the quick response. I tried the logic you provided it works fine for most but for a few numbers for example 10034 is printed as 100.0000.00.34 etc. I'm trying to understand your code in the meanwhile, just wanted to inform you as well. Appreciate your help. – Anusha Nov 15 '16 at 12:18
  • it should be printed as 100.34 as i have tried just now... you need to give me a bigger sample having such thing.. one more thing, would the numbers be separated by space ? space10034space like that ? – Mustofa Rizwan Nov 15 '16 at 12:21
  • In my regex, there is no capture group... all i am doing is matching the numbers.... matcher.group(0) matches the entire regex... which in this case is only digits... what i am wondering is , if you have used my format of "00.00' then how come you get "100.0000.00.34" as result.. it would help you can give me the full sample string over which you have tried the code – Mustofa Rizwan Nov 15 '16 at 12:31
  • Yes, they will be separated by space. I'm trying to read stackoverflow.com/questions/16517689/… to understand what groups are in pattern matching in the meanwhile and I'm also excited. – Anusha Nov 15 '16 at 12:32
  • The sample I used is this: [report_single_fill_limit] Customer: 270009235, from Partner: EM_WALLET, filled: 8001 cents which exceeds the allowed single fill limit of: 8000. So, I added spaces before and after " \\d+ " the regex and it worked. – Anusha Nov 15 '16 at 12:38
0
  1. Split the string into word arrays using space basis
  2. Check each word whether it's a proper number or not.
  3. if it's a number set a flag and on flag basis apply your logic after applying the logic replace the word into the array
  4. print the whole array as string or concatenate as per your requirement
mayank agrawal
  • 628
  • 5
  • 20
0

Using Pattern and Matcher you can find the positions of the numbers.

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(yourStringGoesHere);

while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end() + " ");
    System.out.println(matcher.group());
}

Then you can use substring to add the period.

Community
  • 1
  • 1
AleSod
  • 422
  • 3
  • 6
0

I found it here

public double isNumeric(String str){
  String number="";
  Double num=0;
  for (char c : str.toCharArray()){
      if (Character.isDigit(c)){
         number+=c;
      }
  }
  num = Double.parseDouble(number);
  return num; //Then you can do whatever you want with this
}

Sorry english :/

Community
  • 1
  • 1
rencinas
  • 142
  • 1
  • 8