[Python] How to write a regex to match strings where every distinct character occurs the same...

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a challenge where I need to write a single regex pattern to match strings that satisfy the following property:

    Every distinct character in the string appears exactly the same number of times.

    For example:

    "arraigning" is valid because characters a, r, i, g, n all appear exactly 2 times.

    "deed" is valid because d and e both appear 2 times.

    "edified" is invalid because character counts differ.

    The problem is: I want a regex pattern that matches all such strings and rejects others.

    Constraints:

    The string can contain any characters (not limited to a fixed set).

    The length of the string can be variable (up to some limit, e.g., 62 chars).

    The regex engine is Python's regex module (supports recursion and named groups).

    I suspect this might not be feasible with standard regex, but I want to know if there is a clever pattern or advanced technique that can do this, especially with Python's regex module.

    If it's impossible, please explain why and suggest the best alternative approaches.

    Thanks in advance!

    5. "Tally"
    Alright, time for a real challenge. This one should actually be difficult!


    match_words = [
    "arraigning", "nonordered", "abadbacdcacbdbdc", "mesosome", "ananna",
    "unendued", "tromometer", "caucasus", "intestines", "i", "deed",
    "horseshoer", "happenchance", "reappear", "deeded", "pullup",
    "arraigning", "testes", "mononymy", "scintillescent", "couscous"
    ]

    not_match_words = [
    "edified", "unreverberating", "underpass", "interinsert", "pilfered",
    "nippiness", "gregarinian", "deicide", "nonaristocratic", "rototiller",
    "ozonizing", "museums", "backbreaker", "interradiated", "antistalling",
    "naturalize", "equitriangular", "reparticipate", "ppd", "miasmas", "cabbage"
    ]


    Generate satisfying regex with maximum allowable length is 62

    Just CTF Challenge about regex

    Continue reading...

Compartilhe esta Página