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

Regex Help – what can do to make it generic

I have the following sentence

When the Forward Sensing Camera (FSC) detects a vehicle ahead or pedestrian and determines
that a collision with the object is unavoidable while the vehicle is driven at a vehicle
speed of about 4 to 80 km/h (2 to 50 mph) if the object is a vehicle ahead and 2 to 50km/hr if the object is pedestrian

My goal is to get all the speed ranges. Currently, I am using the regex

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

\d+ to \d+\s?(km\/hr|km\/h| mph)

The only issue is that I have hard-coded a to in the regex. The speed could also be specified as 5 – 25 kmph.

I am lost as to what a generic character sequence could be to cater to anything between two numbers

>Solution :

You can make the k optional and use an alternation:

\b\d+ (?:-|to) \d+\s?(?:km\/hr?| k?mph)\b

The pattern matches:

  • \b A word boundary
  • \d+ Match 1+ digits and
  • (?:-|to) Match either - or to
  • \d+\s? Match 1+ digits with an optional whitespace char
  • (?: Non capture group for the alternatives
    • km\/hr?| k?mph Match either km/h km/hr mph kmph
  • ) Close the group
  • \b A word boundary

See a regex101 demo

Note that there is also a space in k?mph which you match 2 spaces as there is also \s?

If you don’t want 2 spaces, you could write it as:

\b\d+ (?:-|to) \d+(?: ?km\/hr?| k?mph)\b
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