Regex Problem Group Name Redefinition?

后端 未结 3 1100
情书的邮戳
情书的邮戳 2021-02-05 17:32

So I have this regex:

(^(\\s+)?(?P(\\w)(\\d{7}))((01f\\.foo)|(\\.bar|\\.goo\\.moo\\.roo))$|(^(\\s+)?(?PR1_\\d{6}_\\d{6}_)((01f\\.foo)|(\         


        
3条回答
  •  生来不讨喜
    2021-02-05 18:07

    No, you can't have two groups of the same name, this would somehow defy the purpose, wouldn't it?

    What you probably really want is this:

    ^\s*(?P\w\d{7}|R1_(?:\d{6}_){2})(01f\.foo|\.(?:bar|goo|moo|roo))$
    

    I refactored your regex as far as possible. I made the following assumptions:

    You want to (correct me if I'm wrong):

    • ignore white space at the start of the string
    • match either of the following into a group named "NAME":
      • a letter followed by 7 digits, or
      • "R1_", and two times (6 digits + "_")
    • followed by either:
      • "01f.foo" or
      • "." and ("bar" or "goo" or "moo" or "roo")
    • followed by the end of the string

    You could also have meant:

    ^\s*(?P\w\d{7}01f|R1_(?:\d{6}_){2})\.(?:foo|bar|goo|moo|roo)$
    

    Which is:

    • ignore white space at the start of the string
    • match either of the following into a group named "NAME":
      • a letter followed by 7 digits and "01f"
      • "R1_", and two times (6 digits + "_")
    • a dot
    • "foo", "bar", "goo", "moo" or "roo"
    • the end of the string

提交回复
热议问题