Home > Net >  How to simplify regex \d{2}:\d{2} ? ie to match string 10:00
How to simplify regex \d{2}:\d{2} ? ie to match string 10:00

Time:12-29

Current regex: \/bookingkamaroperasi\/((?:\d{2}-){2}\d{4} (?:\d{2}:\d{2}))\/\d{2}

My goal is to match string: /bookingkamaroperasi/10-10-1999 10:00/60. The problem is, the regex is too long. Is there a way to simplify it?

CodePudding user response:

  1. You can switch \d{2} into \d\d, saves 4 characters in total.
  2. Your second non-capturing group does not need to be a group.
  3. Your first non-capturing group can be expanded to \d\d-\d\d- which is shorter than the original.

This gives: \/bookingkamaroperasi\/(\d\d-\d\d-\d{4} \d\d:\d\d)\/\d\d https://regexr.com/75bq1

Also escaping forward slashes is not always necessary depending on the regex engine you are using.

  • Related