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