Я пытаюсь найти шаблон регулярных выражений для следующих примеров (в python)
строка должна начинаться с числа от 1 до 99 и может иметь 0, 1, 2 или 3 *
маркеры в конце. все остальное недопустимо
""
-> NOK (должно содержать хотя бы число) "1"
-> ok "99"
-> нормально "100"
-> NOK (максимум 99)
"1*"
-> нормально
"1**"
-> нормально "1***"
-> хорошо "1****"
-> NOK (макс. 3 * s)
"99***"
-> нормально
"*1"
-> NOK (* только сзади)
"1*1"
-> NOK ( * только сзади) "1 *"
-> NOK (содержит пробел: не ди git или *)
Я получил это далеко: "^[0-9]{1,2}|^([0-9]{1,2})(\Z\*)"
но это не Не поймать случаи "1*1"
или "1 *"
: (
это код python и тесты:
import re
class ControlePunt:
@staticmethod
def create(punt):
should_start_with_number_and_may_end_with_marker = "^[0-9]{1,2}|^([0-9]{1,2})(\Z\*)"
if re.match(should_start_with_number_and_may_end_with_marker, punt):
return ControlePunt(punt)
else:
return NoControlePunt()
def __init__(self, punt):
self.punt = punt
class NoControlePunt(ControlePunt):
def __init__(self):
super().__init__("")
import unittest
from ControlePunt import ControlePunt, NoControlePunt
class ValidControlePuntTests(unittest.TestCase):
def test_valid_numeric(self):
self.assertEqual(ControlePunt.__name__, type(ControlePunt.create("21")).__name__)
def test_valid_numeric_with_1_marker(self):
self.assertEqual(ControlePunt.__name__, type(ControlePunt.create("21*")).__name__)
def test_valid_numeric_with_2_markers(self):
self.assertEqual(ControlePunt.__name__, type(ControlePunt.create("21**")).__name__)
def test_valid_numeric_with_3_markers(self):
self.assertEqual(ControlePunt.__name__, type(ControlePunt.create("21***")).__name__)
class InvalidControlePuntTests(unittest.TestCase):
def test_empty(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("")).__name__)
def test_only_marker(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("*")).__name__)
def test_non_numeric(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("a1")).__name__)
def test_marker_before_numbers(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("*1")).__name__)
def test_marker_surrounded_by_numbers(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("1*1")).__name__)
def test_containing_illegal_characters(self):
self.assertEqual(NoControlePunt.__name__, type(ControlePunt.create("1 *")).__name__)
if __name__ == '__main__':
unittest.main()