如何在python中创建带重复的正则表达式模式

时间:2017-02-17 20:50:57

标签: python regex extract

我有像" hereis-SSN1234_12-12","来自-SSN2343_SSN3223-21"," SSN1235_SSN021_SSN3122-123",我想提取子串是:

from rest_framework.pagination import PageNumberPagination
from .pagination import CustomNumberPagination


class Testing(generics.GenericAPIView):

    queryset = Testing.objects.all()
    serializer_class = TestingSerializer
    pagination_class = CustomNumberPagination

    def get(self, request):
        queryset = self.get_queryset()
        page = self.request.query_params.get('page')
        if page is not None:
            paginate_queryset = self.paginate_queryset(queryset)
            serializer = self.serializer_class(paginate_queryset, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)

SSN部件可以重复多次。

如何构建单个正则表达式模式来查找匹配的子字符串?

1 个答案:

答案 0 :(得分:4)

使用括号创建一个组,并在该组之后添加一个量词:

(?:SSN\d+(?:_\d+)?_?)+