结合不同的表达方式

时间:2017-03-27 09:57:08

标签: regex

我仍然是正则表达式的新手,并希望将不同的表达式组合在一起。现在所有这些表达式在单独使用时都有效,但我似乎无法将它们组合成一个有效的字符串。

我想只在开头没有空格的情况下匹配一个%的字符串。

实施例

%string  => Match
% string => No Match
%%string => No Match

现在我有这些表达方式:

(^[^%]*%[^%]*$) - Matches only one % but with whitespace
(%\S+)([^\n]+)  - Matches strings with the % and no whitespace behind but also matches the %% string that shouldn't be matched

我尝试将它们与另一个

复制它们结合起来
(^[^%]*%[^%]*$)(\S+)([^\n]+)

但这不起作用。我知道在组合它们时我做错了什么,我只是不知道是什么。

1 个答案:

答案 0 :(得分:2)

您可以使用

post

请参阅regex demo

<强>详情:

  • def post(self, request, *args, **kwargs): form_class = self.get_form_class() form = self.get_form(form_class) if request.user.is_staff: if form.is_valid(): emails = ClientContact.objects.all() # Look here list_of_emails = [] for email in emails: email_lst = [] email_lst.append(email.user.email) list_of_emails.append(email_lst) return excel.make_response_from_array(list_of_emails, "xls", file_name="export_client_mail", status=200) else: return HttpResponseBadRequest() else: return HttpResponseForbidden() - 字符串锚的开始
  • ^%[^\s%]*$ - 百分号
  • ^ - 一个否定的字符类,匹配除空格和%
  • 之外的零个或多个字符
  • [^\s%]* - 字符串锚点结束