Matches any string that matches the specified regular expression.
| 353 | |
| 354 | |
| 355 | class Matching(Also): |
| 356 | """Matches any string that matches the specified regular expression.""" |
| 357 | |
| 358 | def __init__(self, pattern, regex, flags=0): |
| 359 | assert isinstance(regex, bytes) or isinstance(regex, str) |
| 360 | super().__init__(pattern) |
| 361 | self.regex = regex |
| 362 | self.flags = flags |
| 363 | |
| 364 | def __repr__(self): |
| 365 | s = repr(self.regex) |
| 366 | if s[0] in "bu": |
| 367 | return s[0] + "/" + s[2:-1] + "/" |
| 368 | else: |
| 369 | return "/" + s[1:-1] + "/" |
| 370 | |
| 371 | def _also(self, value): |
| 372 | regex = self.regex |
| 373 | |
| 374 | # re.match() always starts matching at the beginning, but does not require |
| 375 | # a complete match of the string - append "$" to ensure the latter. |
| 376 | if isinstance(regex, bytes): |
| 377 | if not isinstance(value, bytes): |
| 378 | return NotImplemented |
| 379 | regex += b"$" |
| 380 | elif isinstance(regex, str): |
| 381 | if not isinstance(value, str): |
| 382 | return NotImplemented |
| 383 | regex += "$" |
| 384 | else: |
| 385 | raise AssertionError() |
| 386 | |
| 387 | return re.match(regex, value, self.flags) is not None |
no outgoing calls
no test coverage detected
searching dependent graphs…