Python regex: matching vehicle speed without highway names

I am trying to match the descriptions of the speed of travel tickets, for example, text strings:

"L 16-25MPH" should return 2 groups: 16, 25
"LMT ACC 6-10" should return 2 groups: 6, 10
"6 OVER" should return 1 group: 6

I am fine with all of the above situations, but I have there are problems for lines with numbers that are not related to speed, for example:

"LIMITED ACCESS SPEED I-75" must not return matches.
The closest expression I can get to capture everything I need is: ((?<!\w-)\d+)[^\d]*((?<!\w-)\d+)?which will correspond to 1 group: 5 using the python regex engine

Currently, it is safe to assume that the letter, and then the hyphen ( \w-), is what I am trying to use to exclude the negative answer, I'm just not sure how to group more than one digit ( \d+) to use the negative answer.

+4
source share
2 answers

Description

([0-9]+)(?:-([0-9]+)|\s*over)

Regular expression visualization

** To see the image better, just right-click on the image and select a view in a new window

This regex will do the following:

  • Matches speed related numbers
  • avoid numbers that are part of road names

Example

Live demo

https://regex101.com/r/hE5dL4/2

Sample text

Note: edge about I-75

'm trying to match speed descriptions of highway tickets, for example,text lines:

"L A 16-25MPH" should return 2 groups: 16, 25 
"LIMITED ACCESS SPEED I-75" should return no matches.
"LMT ACC 6-10" should return 2 groups: 6, 10 
"6 OVER" should return 1 group: 6

I'm OK with all of the above situations, but I run into issues for strings with numbers that aren't related to speed, for example:

"LIMITED ACCESS SPEED I-75" should return no matches.

Matching Examples

MATCH 1
1.  [89-90] `16`
2.  [91-93] `25`

MATCH 2
1.  [193-194]   `6`
2.  [195-197]   `10`

MATCH 3
1.  [231-232]   `6`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    -                        '-'
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      [0-9]+                   any character of: '0' to '9' (1 or
                               more times (matching the most amount
                               possible))
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    over                     'over'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
+2

lookbehind ( . Python), , \d+, .

lookbehind , , , I RT ( ). , , ..

, ( ): (?<!i|rt)(\d+)

+3

All Articles