CodexBloom - Programming Q&A Platform

Regex scenarios to Match IPv6 Addresses with Embedded Zone Identifiers in Python

👀 Views: 46 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-06
regex ipv6 python Python

I'm sure I'm missing something obvious here, but I've looked through the documentation and I'm still confused about I'm working on a Python script that needs to validate and extract IPv6 addresses, including those with embedded zone identifiers like 'fe80::1ff:fe23:4567:890a%eth0'... I thought I could use the following regex pattern: ```python import re ipv6_pattern = r'([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|([0-9a-fA-F]{1,4}:){1}(:[0-9a-fA-F]{1,4}){1,6}|:((:[0-9a-fA-F]{1,4}){1,7}|:) match = re.match(ipv6_pattern, 'fe80::1ff:fe23:4567:890a%eth0') if match: print('Valid IPv6 address') else: print('Invalid IPv6 address') ``` However, the regex seems to unexpected result when I include the zone identifier after the '%' character. The match always returns `None`, indicating that it doesn't recognize the pattern correctly, which leads to the output 'Invalid IPv6 address'. I've tried adding an optional non-capturing group for the zone identifier at the end of the pattern with: ```python (?:%[0-9a-zA-Z_]+)? ``` But this still doesn't work as expected. I've even tested it with `re.search` and `re.fullmatch`, but the results are the same. Am I missing something in my regex, or is there a better approach to handle this in Python 3.9? Any insights would be greatly appreciated! My development environment is Ubuntu. What am I doing wrong? For context: I'm using Python on Windows. Thanks in advance! This is my first time working with Python latest. Any ideas how to fix this?