{"id":21902518,"url":"https://github.com/jmininger/mini-regex","last_synced_at":"2025-03-22T06:25:05.714Z","repository":{"id":240404747,"uuid":"148396673","full_name":"jmininger/mini-regex","owner":"jmininger","description":"Regular Language -\u003e NFA -\u003e DFA simulation. Regular expression engine that matches patterns against input strings","archived":false,"fork":false,"pushed_at":"2019-06-08T20:21:49.000Z","size":97,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-27T06:46:25.985Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmininger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-12T00:20:04.000Z","updated_at":"2023-07-30T00:00:16.000Z","dependencies_parsed_at":"2024-05-18T16:41:16.354Z","dependency_job_id":"494ecfc1-b8ee-47cf-b488-db4664609ce7","html_url":"https://github.com/jmininger/mini-regex","commit_stats":null,"previous_names":["jmininger/mini-regex"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmininger%2Fmini-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmininger%2Fmini-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmininger%2Fmini-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmininger%2Fmini-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmininger","download_url":"https://codeload.github.com/jmininger/mini-regex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244915714,"owners_count":20531271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T15:19:29.454Z","updated_at":"2025-03-22T06:25:05.693Z","avatar_url":"https://github.com/jmininger.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mini Regex\n\nA simple regular expression engine that parses regular expressions, converts them into an\nNFA(non-deterministic finite automata) and runs it on input strings to find\nmatches.   \n\n### Patterns supported: \n  - `ab` -- union\n  - `a|b` -- or\n  - `a*` -- Kleene star (0 or more matches)\n  - `.` -- Metachar\n  - `(ab(c|d))|e` -- nested expressions\n  - `[^A-Zabc0-9]` -- regex classes with range and negation\n  - `?` -- 0 or 1 match\n  - `+` -- 1 or more matches\n  - `\\\\.` -- Backslash to escape special chars\n\n### Context Free Grammar for mini regex: \n```\nExp -\u003e Term Exp`\nExp`-\u003e '|'Exp | empty\nTerm -\u003e Factor Term`\nTerm`-\u003e Term | empty\nFactor -\u003e C Factor`\nFactor`-\u003e '*'|'?'|'+'| empty\nC -\u003e CharType | ( Exp )\nCharType -\u003e Class | Char | MetaChar\nChar -\u003e All ascii chars not including metachars or metachars with front slash\nMetaChars -\u003e . | \\\\b | '|' | * | ? | + | ( | ) | [ | ]\nClass -\u003e '[' InnerClass ']' | '[^' InnerClass ']'\nInnerClass -\u003e Range | ClassChars\nRange -\u003e ClassChars - ClassChars\nClassChars -\u003e Ascii chars, no special chars\n```\n\n#### To run tests:\n```\n$ python3 -m unittest discover -s test/\n```  \n\n#### To run example:\n```\n$ python3 example.py\n```\n\n### Implementation Details:\n  - Uses a handmade recursive descent parser (because it would be cheating to use\n    regular expressions in a parser for regular expressions)\n  - As the parser goes through the pattern, it uses thompsons constructions\n    (see the wikipedia article linked in thompson_construction.py), to build up\n    a Non-Deterministic-Finite-Automata (NFA). [See more here](https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton)\n  - A traditional regex only supports the (implicit) concat operator, pipe/union operator, and the\n    kleene star operator. To get around this, I use predicate-based\n    \"transitions\" between nodes (see transitions.py) that allows me to support\n    things like metachars, and regex character classes. Other ops (+, ?) are\n    derived using the three basic operations above.\n  - When run, an NFA can be in multiple states simultaneously. Some\n    implementations use backtracking to handle this. Instead, I used what I\n    called a \"DFA State\" which holds a set of NFA substates that are\n    \"active\". Each new input character (from the string being searched)\n    either kills or advances each substate in this set. When advanced, a\n    substate also spawns multiple new substates, for each of the nfa's nodes\n    reachable by only epsilon transitions from the current node.\n    \"epsilon\" transitions. \n  - If at anypoint the DFAState holds a substate that is the NFA's endstate,\n    the DFARunner has found a \"matching string\".\n  - If at anypoint the DFAState no longer holds any active substates, the\n    DFARunner is completed\n  - The is_greedy property of the MiniRegex class (true by default) determines\n    whether the DFARunner needs to be \"complete\" before returning a match that\n    has been found. \n\n#### TODO:\n  - groups\n  - ^ match at the beginning\n  - $ match at the end of the string$\n  - \\w, \\W, \\b...etc whitespace and newline escapes\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmininger%2Fmini-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmininger%2Fmini-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmininger%2Fmini-regex/lists"}