Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert POSIX regex to python regex #18

Open
larsbuntemeyer opened this issue Aug 27, 2024 · 1 comment
Open

convert POSIX regex to python regex #18

larsbuntemeyer opened this issue Aug 27, 2024 · 1 comment

Comments

@larsbuntemeyer
Copy link
Contributor

some prototype code:

import re

def convert_posix_to_python(posix_regex):
    """
    Convert common POSIX regular expressions to Python regular expressions.
    """
    # Dictionary of POSIX to Python character class conversions
    posix_to_python_classes = {
        r'\[\[:alnum:\]\]': r'[a-zA-Z0-9]',
        r'\[\[:alpha:\]\]': r'[a-zA-Z]',
        r'\[\[:digit:\]\]': r'\d',
        r'\[\[:xdigit:\]\]': r'[0-9a-fA-F]',
        r'\[\[:lower:\]\]': r'[a-z]',
        r'\[\[:upper:\]\]': r'[A-Z]',
        r'\[\[:blank:\]\]': r'[ \t]',
        r'\[\[:space:\]\]': r'\s',
        r'\[\[:punct:\]\]': r'[!"#$%&\'()*+,\-./:;<=>?@[\\\]^_`{|}~]',
        r'\[\[:word:\]\]': r'\w'
    }
    
    # Replace POSIX character classes with Python equivalents
    for posix_class, python_class in posix_to_python_classes.items():
        posix_regex = re.sub(posix_class, python_class, posix_regex)
    
    # Replace POSIX quantifiers with Python equivalents
    posix_regex = posix_regex.replace(r'\{', '{').replace(r'\}', '}')
    
    return posix_regex

# Example usage
posix_regex = r'r[[:digit:]]\{1,\}i[[:digit:]]\{1,\}p[[:digit:]]\{1,\}f[[:digit:]]\{1,\}$'
python_regex = convert_posix_to_python(posix_regex)
print("Converted Python regex:", python_regex)
@sol1105
Copy link
Collaborator

sol1105 commented Sep 20, 2024

That looks nice, we can add this in the next PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants