简体   繁体   中英

Repeating large elements in a regular expression

I have the following regular expression:

_parser = re.compile('''
                    (?P<a>-?[0-9]+(\.[0-9]+)?(/-?[0-9]+(\.[0-9]+)?)?)?x[+\-]
                    (?P<b>-?[0-9]+(\.[0-9]+)?(/-?[0-9]+(\.[0-9]+)?)?)?y[+\-]
                    (?P<c>-?[0-9]+(\.[0-9]+)?(/-?[0-9]+(\.[0-9]+)?)?)?=0
                    ''', re.VERBOSE)

This quite obviously has a lot of repetition in it, so it got me wondering, what's the syntax (if it exists) to repeat blocks of similar expressions in a single expression?

You can do this:

Surround [0-9]+(\\.[0-9]+)?(/-?[0-9]+(\\.[0-9]+) in another capture group, and then refer to it with \\2 , since it's the second open-paren in the regex:

_parser = re.compile('''
     (?P<a>-?([0-9]+(\.[0-9]+)?(/-?[0-9]+(\.[0-9]+))?)?)?x[+\-]
              (?P<b>-?\2?)?)?y[+\-]
                (?P<c>-?\2?)?)?=0
                ''', re.VERBOSE)

You could also do this by creating the string of the regex, and then using that string-variable repeatedly. I don't know Python, but in Java it'd be something like this:

String sRegexPiece = "[0-9]+(\\.[0-9]+)?(/-?[0-9]+(\\.[0-9]+)";
String sRegexWhole = "(?P<a>-?" + sRegexPiece + "?)?x[+\-]" + sLS + //sLS: line separator
    "(?P<b>-?" + sRegexPiece + "?)?)?y[+\-]" + sLS + 
    "(?P<c>-?" + sRegexPiece + "?)?)?=0" + sLS + 
    "'''";
Pattern p = Pattern.compile(sRegexWhole);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM