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

\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

Leave a Reply