Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading