Reputation: 37
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:
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
Upvotes: 1
Views: 504
Reputation: 626748
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.Upvotes: 1