正则表达式python条件

时间:2021-06-21 06:58:22

标签: python regex tkinter

我用 tkinter 编写了一个应用程序。现在我想在用户使用它时在输入框中添加条件/模式。我想强制用户使用这些模式:

    class SoloAdmin(admin.ModelAdmin):
        list = ('user','player1_pubg_id','player1_pubg_name')


    admin.site.register(Profile)
    admin.site.register(solo_21_6_2021,SoloAdmin)

我用这个程序来检查我的正则表达式

The entry must begin by this pattern `^#\d+\s[A-I]\d+(,|;|-|)`
if there is a `,` or `-` in the string, the pattern must be the same plus `[A-I]\d+`
If there is `;` in the string, redo the first pattern
if after  `^#\d+\s[A-I]\d+(,|;|-|)` there is nothing, do nothing
I used regex conditions but nothing happened. 
I use this `^#\d+\s[A-I]\d+(,|;|-|)(?:(?=,)[A-I]\d+)`

1 个答案:

答案 0 :(得分:1)

请注意,[A-I]Q 中的 Q7 不匹配。您可以使用 [A-Z] 扩展范围并为第一个和第二个模式使用重复组。

你可能会使用

#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*)*

模式匹配:

  • #\d+\s[A-Z]\d+ 匹配 # 1+ 位数字、一个空格字符和 1+ 位数字
  • (?:[,-][A-Z]\d+)* 可选择重复匹配 ,- 字符 A-Z 和 1+ 位数字
  • (?: 非捕获组
    • ;#\d+\s[A-Z]\d+ 匹配 ; 和第一个模式
    • (?:[,-][A-Z]\d+)* 可选择重复匹配第二个模式
  • )* 关闭非捕获组并可选择重复整个部分

Regex demo

或者如果您还想允许空格:

#\d+\s[A-Z]\d+(?:[,-]\s?[A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-]\s?[A-Z]\d+)*)*

Regex demo

相关问题