Home > database >  java - regex pattern matching days, hours, minutes, seconds
java - regex pattern matching days, hours, minutes, seconds

Time:08-26

I have a text file with a lot of data and some of that is time with combination of days, hours, minutes and seconds. Examples are listed below.

Examples:

  • 2 days, 3 hours, 24 minutes, 16 seconds
  • 1 days, 4 minutes, 3 seconds
  • 4 hours, 17 minutes, 56 seconds
  • 4 hours, 17 seconds
  • 2 hours, 3 minutes
  • 3 minutes, 15 seconds
  • 45 seconds

I am trying to replace all mention of times to a generic string like "TimeString". I have written my own regex, but it's not working as expected

\\d( days)?(, \\d)?( hours)?(, \\d)?( minutes)?(, \\d)?( seconds)?

For this, all numbers are getting replaced along with times. For example, if something says "26 orders", it will be replaced as "TimeString orders" which should not happen

CodePudding user response:

You can use

\d \s*(?:day|hour|minute|second)s?(?:\s*,\s*\d \s*(?:day|hour|minute|second)s?)*

See the regex demo

Details:

  • \d \s*(?:day|hour|minute|second)s? - one or more digits, zero or more whitespaces, day or hour or minute or second and then an optional s char
  • (?:\s*,\s*\d \s*(?:day|hour|minute|second)s?)* - zero or more sequences of a comma enclosed with zero or more whitespaces and then the same pattern as above.
  • Related