Regex Distance Breakdown
We use a regex expression based on what type of video it is.
distance_field_regex = r'(?<=^)-?\d?\d?\d?\d ?[\.,]? ?[\d@O][\d@O](?= ?[Mm])'
This pattern is designed to match a number format at the start of a string, potentially with specific suffixes like "M" or "m". Let's examine each part in detail:
Breakdown of Each Component
(?<=^) # Positive lookbehind for the start of the string (^).
Ensures that the pattern matches only if it starts right at the beginning of the string. Example: Ensures -123 would match at the start of a string like "-123 m", but not if it were part of a larger string (e.g., "abc -123 m").
-? # Matches an optional minus sign (-).
This allows the number to be either negative or positive.
\d?\d?\d?\d: Matches up to four optional digits (\d).
Each \d? allows for a single digit to appear zero or one time. This segment allows anywhere from 0 to 4 digits at the beginning. Examples: -1234, 567, 89, or even an empty match here.
?: Matches an optional space.
Allows for a space after the first set of digits. Examples: 1234 , 45 .
[\.,]?: Matches an optional period (.) or comma (,).
This allows for either a decimal point or a thousands separator. Examples: 1234., 123,.
?: Matches another optional space.
Allows for a space after the decimal point or comma, if present. Examples: 1234. , 45, .
[\d@O][\d@O]: Matches two characters, where each character can be:
A digit (\d), The character @, or The character O. This part could be helpful for cases where digits might sometimes be represented as @ or O due to OCR errors or stylization.
Examples: 12, O@, 7O.
(?= ?[Mm]): Positive lookahead for an optional space followed by either "M" or "m".
Ensures that the matched pattern is followed by an "M" or "m" (case-insensitive) after an optional space. We were using [M|m] and we should not be. However, [M|m] here is not quite correct since square brackets ([]) create a character class, so it actually matches any single character among "M", "m", or "|" (the | character would be included mistakenly). To correctly match "M" or "m" specifically, it should instead be written as:
(?= ?[Mm])