'python regex - prevent regex finding pattern when surrounded by other number ( 00/00 pattern)

example text:

Tuesday
05/04
09:00/22:00 13 Hours

current regex: find_result = re.findall('\d+/\d+', fixed_lines1)

my current regex finds this pattern in a string 00/00. A problem arrises as in my string is also have "09:00/22:00" and the regex sees 09:00/22:00 - how do i prevent it from finding this pattern once surrounded by other numbers



Solution 1:[1]

You'll want to use a negative lookahead and a negative lookbehind, to avoid matching numbers separated by a slash if there's a colon before or after it. However, there's a catch:

import re

fixed_lines1 = """Tuesday
05/04
09:00/22:00 13 Hours"""

# this is what you had, result ['05/04', '00/22']
find_result = re.findall('\d+/\d+', fixed_lines1)
print(find_result)

# with negative lookbehind and negative lookahead, 
# you can avoid matching if there's a colon there.
# However, the result is ['05/04', '0/2']
find_result = re.findall('(?<!:)\d+/\d+(?!:)', fixed_lines1)
print(find_result)

# to avoid matching if there's a colon *or* a number
# before or after the match, you want the following,
# with the result ['05/04']
find_result = re.findall('(?<![:\d])\d+/\d+(?![:\d])', fixed_lines1)
print(find_result)

The negative lookbehind given here looks for anything that's not a colon, or a number. And the same for the negative lookahead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grismar