Convert a list to a regex string for matching a directive. Want possible matching values to be from longest to shortest. This prevents the possibility of a match occurring for a value that also a substring of a larger value that should have matched (e.g., 'abc' matc
(self, to_convert, directive, altregex=None)
| 424 | base.__setitem__('c', self.pattern(self.locale_time.LC_date_time)) |
| 425 | |
| 426 | def __seqToRE(self, to_convert, directive, altregex=None): |
| 427 | """Convert a list to a regex string for matching a directive. |
| 428 | |
| 429 | Want possible matching values to be from longest to shortest. This |
| 430 | prevents the possibility of a match occurring for a value that also |
| 431 | a substring of a larger value that should have matched (e.g., 'abc' |
| 432 | matching when 'abcdef' should have been the match). |
| 433 | |
| 434 | """ |
| 435 | to_convert = sorted(to_convert, key=len, reverse=True) |
| 436 | for value in to_convert: |
| 437 | if value != '': |
| 438 | break |
| 439 | else: |
| 440 | return '' |
| 441 | regex = '|'.join(re_escape(stuff) for stuff in to_convert) |
| 442 | if altregex is not None: |
| 443 | regex += '|' + altregex |
| 444 | return '(?P<%s>%s)' % (directive, regex) |
| 445 | |
| 446 | def pattern(self, format): |
| 447 | """Return regex pattern for the format string. |