find consequent row of numbers (separated by non alphabetic characters) and count them

Viewed 153

I wanted to know how to achieve the following scenarios with java regex:

  • find all consequent numbers in a string which are not separated by alphabetic characters, count them, and if the number of digits is between 4 and 5(5 included), then replace them with "*"

Examples:

  • "0000" will become "****"
  • "any text 000 00 more texts" will become "any text ***** more texts"..notice that the space is removed
  • "any text 000 00 more texts 00" will become "any text ***** more texts 00"

  • "any text 000 00 more texts 00 00" will become "any text ***** more texts ****"

  • "any text 00-00 more texts 00_00" will become "any text **** more texts ****"

To find the numbers I have tried:

  • (\d*)(?=[^a-bA-Z]*) and

  • (\d*)([^a-bA-Z])(\d*)

  • (\d*)([^a-bA-Z])(\d*)

But even matching the cases does not work.

I need more understanding of how to do regex operations.

4 Answers
Related