{"id":18441545,"url":"https://github.com/zokis/humanregex","last_synced_at":"2025-04-07T22:32:21.264Z","repository":{"id":32173899,"uuid":"35747249","full_name":"zokis/HumanRegex","owner":"zokis","description":"Human Regex","archived":false,"fork":false,"pushed_at":"2019-11-20T20:54:01.000Z","size":43,"stargazers_count":14,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T01:41:20.421Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zokis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-17T01:34:54.000Z","updated_at":"2021-11-01T09:43:23.000Z","dependencies_parsed_at":"2022-06-26T18:08:11.177Z","dependency_job_id":null,"html_url":"https://github.com/zokis/HumanRegex","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zokis%2FHumanRegex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zokis%2FHumanRegex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zokis%2FHumanRegex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zokis%2FHumanRegex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zokis","download_url":"https://codeload.github.com/zokis/HumanRegex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247741426,"owners_count":20988399,"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-06T06:38:19.974Z","updated_at":"2025-04-07T22:32:18.199Z","avatar_url":"https://github.com/zokis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nHumanRegex\n==========\n\n\n[![Build Status](https://travis-ci.org/zokis/HumanRegex.svg?branch=master)](https://travis-ci.org/zokis/HumanRegex) [![PyPI version](https://badge.fury.io/py/hre.svg)](http://badge.fury.io/py/hre)\n\n## Install\n\n```sh\npip install hre\n```\n\nAPI to make it easier to use regex\n\n``RE('your regex here')``\n\nReturns a Callable object that facilitates verification of matches\n\nIt allows the combination of regex to form new regex using the operators \u0026 and |\n\nFlags Support\n\nIt has a verbose api to create regex and with shortcuts, both supported the combinations\n\n\n## Examples\n\n### Testing if the string contains digits\n```python\nfrom hre import RE\n\nmy_re = RE('[0-9]+')\nif bool(my_re('number: 25')):\n    print('Regex Ok')\n# \u003e\u003e Regex Ok\nmy_match = my_re('number: 25')\nif my_match[0] == '25':\n    print('25')\n# \u003e\u003e 25\nif RE('(?P\u003cnumber\u003e[0-9]+)')('number: 25')['number'] == '25':\n    print('number 25')\n# \u003e\u003e number 25\n```\n\n### Tests if the email is valid and captures the account and the provider\n```python\nmy_re = RE(r'(?P\u003caccount\u003e[A-Za-z0-9+_.]+)\\@(?P\u003cprovider\u003e[A-Za-z0-9]+)\\..+')\nmy_match = my_re('my_email.1+github@Provider.com')\nprint my_match['account']\n# \u003e\u003e my_email.1+github\nprint my_match['provider']\n# \u003e\u003e Provider\n```\n\n### Replacing strings\n```python\nfrom hre import RE\n\nprint RE('(?:red)').replace(\"violets are red\", 'blue')\n# \u003e\u003e violets are blue\n```\n\n### Using verbal expressions for the same examples\n```python\nfrom hre import HumanRegex as HR\n\nmy_re = HR().digits()\nif bool(my_re('number: 25')):\n    print('Regex Ok')\n# \u003e\u003e Regex Ok\nmy_match = my_re('number: 25')\nif my_match[0] == '25':\n    print('25')\n# \u003e\u003e 25\nif HR().digits(name='number')('number: 25')['number'] == '25':\n    print('number 25')\n# \u003e\u003e number 25\n\n```\n\n```python\nfrom hre import HumanRegex as HR\n\naz = ['a', 'z']\nAZ = ['A', 'Z']\n_09 = ['0', '9']\nspecial = '_.+'\n\nmy_re = HR().ranges(\n    AZ, az,\n    _09, special,\n    name='account'\n).then('@').ranges(\n    AZ, az, _09,\n    name='provider'\n).then('.').anything()\nmy_match = my_re('my_email.1+github@Provider.com')\nprint my_re\n# \u003e\u003e (?P\u003caccount\u003e([A-Za-z0-9\\_\\.\\+]+))(?:\\@)(?P\u003cprovider\u003e([A-Za-z0-9]+))(?:\\.)(?:.*)\nprint my_match['account']\n# \u003e\u003e my_email.1+github\nprint my_match['provider']\n# \u003e\u003e Provider\n```\n\n```python\nfrom hre import HR\n\nprint HR().find('red').replace(\"violets are red\", 'blue')\n# \u003e\u003e violets are blue\n```\n\n### Combinations\n\n```python\nfrom hre import HR\n\n\nvalids = ['abacate', '42', 'tomate', '25']\n\nvalid_comb = HR().then(valids[0])\n# same as valid_comb = T(valids[0])\nfor valid in valids[1:]:\n    valid_comb |= HR().then(valid)\n    # same as valid_comb |= T(valid)\n\nvalids = ADD(valid_comb, name='valid')\nmy_comb = HR().then('{').start_of_line() \u0026 valids \u0026 HR().then('}').end_of_line()\n# same as my_comb = SOL() \u0026 T('{') \u0026 valids \u0026 T('}') \u0026 EOL()\nprint my_comb\n# \u003e\u003e ^(?:\\{)(?P\u003cvalid\u003e(?:abacate)|(?:42)|(?:tomate)|(?:25))(?:\\})$\nprint my_comb('{42}')['valid']\n# \u003e\u003e 42\nprint my_comb('{abacate}')['valid']\n# \u003e\u003e abacate\nprint my_comb('{invalid}')['valid']\n# \u003e\u003e None\n\nx = HR().then('@').word(name='p')\ny = HR().char(name='c').then('.')\nmy_combination = y \u0026 x\nmy_match = my_combination('x.@zokis')\nprint \"regex: \", my_combination\n# \u003e\u003e regex:  (?P\u003cc\u003e\\w)(?:\\.)(?:\\@)(?P\u003cp\u003e\\w+)\nprint \"c: \", my_match['c']\n# \u003e\u003e c:  x\nprint \"p: \", my_match['p']\n# \u003e\u003e p:  zokis\n```\n\n\n### Examples using shortcuts and combinations\n\n```python\nfrom hre import DS\n\nmy_re = DS()\nprint my_re\n# \u003e\u003e \\d+\n\nif bool(my_re('number: 25')):\n    print('Regex Ok')\n# \u003e\u003e Regex Ok\nmy_match = my_re('number: 25')\nif my_match[0] == '25':\n    print('25')\n# \u003e\u003e 25\n\nmy_named_regex = DS(name='number')\nprint my_named_regex\n# \u003e\u003e (?P\u003cnumber\u003e\\d+)\nif my_named_regex('number: 25')['number'] == '25':\n    print('number 25')\n# \u003e\u003e number 25\n```\n\n```python\nfrom hre import RS, T, AT\n\naz = ['a', 'z']\nAZ = ['A', 'Z']\n_09 = ['0', '9']\nspecial = '_.+'\n\nmy_re = RS(\n    AZ, az, _09, special,\n    name='account'\n) \u0026 T('@') \u0026 RS(\n    AZ, az, _09,\n    name='provider'\n) \u0026 AT()\nmy_match = my_re('my_email.1+github@Provider.com')\nprint my_re\n# \u003e\u003e (?P\u003caccount\u003e([A-Za-z0-9\\_\\.\\+]+))(?:\\@)(?P\u003cprovider\u003e([A-Za-z0-9]+))(?:.*)\nprint my_match['account']\n# \u003e\u003e my_email.1+github\nprint my_match['provider']\n# \u003e\u003e Provider\n```\n\n```python\nfrom hre import F\n\nprint F('red').replace(\"violets are red\", 'blue')\n# \u003e\u003e violets are blue\n```\n\n```python\nd3 = D(quantifier=3)\nd2 = D() * 2\n\nt = T('-')\np = T('.')\ncpf_re = d3 \u0026 p \u0026 d3 \u0026 p \u0026 d3 \u0026 t \u0026 d2\nprint cpf_re\n# \u003e\u003e \\d{3}(?:\\.)\\d{3}(?:\\.)\\d{3}(?:\\-)\\d\\d\nprint bool(cpf_re('412.459.786-08'))\n# \u003e\u003e True\n\ncnpj_re = d2 \u0026 p \u0026 d3 \u0026 p \u0026 d3 \u0026 T('/') \u0026 D(quantifier=4) \u0026 t \u0026 d2\nprint cnpj_re\n# \u003e\u003e \\d\\d(?:\\.)\\d{3}(?:\\.)\\d{3}(?:\\/)\\d{4}(?:\\-)\\d\\d\nprint bool(cnpj_re('76.612.217/0001-14'))\n# \u003e\u003e True\n\ncpf_cnpj_re = G(cpf_re) | G(cnpj_re)\nprint cpf_cnpj_re\n# \u003e\u003e (\\d{3}(?:\\.)\\d{3}(?:\\.)\\d{3}(?:\\-)\\d\\d)|(\\d\\d(?:\\.)\\d{3}(?:\\.)\\d{3}(?:\\/)\\d{4}(?:\\-)\\d\\d)\nprint bool(cpf_cnpj_re('856.324.440-07'))\n# \u003e\u003e True\nprint bool(cpf_cnpj_re('49.347.475/0001-48'))\n# \u003e\u003e True\n```\n\n\n### FLAGs\n\n```python\nmy_re = HR().find('cat')\nmy_match = my_re('CAT or dog')\nprint bool(my_match)\n# \u003e\u003e False\nmy_re = my_re.ignorecase()\nmy_match = my_re('CAT or dog')\nprint bool(my_match)\n# \u003e\u003e True\n\nmy_re = SOL() \u0026 F('DOG')\nmy_match = my_re('CAT or \\ndog')\nprint bool(my_match)\n# \u003e\u003e False\nmy_re = my_re \u0026 FI() | FM()\nmy_match = my_re('CAT or \\ndog')\nprint bool(my_match)\n# \u003e\u003e True\n```\n\n\n### Full API\n\n - column Shortcut: Shortcut Function or Flag Class\n - column Verbose: Verbose method =\u003e HR().x\n - column Example Shortcut: Example using the shortcut functions\n - column Resulting: resulting regex\n - column V: receives \"value\" as a parameter\n - column N: receives \"name\" as named parameter =\u003e Named groups\n - column Q: receives \"quantifier\" =\u003e {x};{x,};{x,y}\n\n| Shortcut | Verbose         | Example Shortcut      | Resulting                  | V | N | Q |\n|----------|-----------------|-----------------------|----------------------------|---|---|---|\n| ADD      | .add            | ADD('[0-9]+')         | ``[0-9]+             ``    | ✓ | ✓ | ✓ |\n| RE       | .add            | RE('[0-9]+')          | ``[0-9]+             ``    | ✓ | ✓ | ✓ |\n| T        | .then           | T('@')                | ``(?:\\@)             ``    | ✓ | ✓ | ✓ |\n| F        | .find           | F('blue')             | ``(?:blue)           ``    | ✓ | ✓ | ✓ |\n| G        | .group          | G(T('A')\u0026#124;T('B')) | ``((?:A)``\u0026#124;``(?:B))`` | ✓ | ✓ | ✗ |\n| A        | .any            | A('0258qaz')          | ``[0258qaz]          ``    | ✓ | ✓ | ✓ |\n| AT       | .anything       | AT()                  | ``(?:.*)             ``    | ✗ | ✓ | ✗ |\n| ATB      | .anything_but   | ATB('0258zaq')        | ``(?:[^0258zaq]*)    ``    | ✓ | ✓ | ✗ |\n| EOL      | .end_of_line    | EOL()                 | ``$                  ``    | ✗ | ✗ | ✗ |\n| MB       | .maybe          | MB('s')               | ``(?:s)?             ``    | ✓ | ✓ | ✗ |\n| MTP      | .multiple       | MTP()                 | ``+                  ``    | ✗ | ✗ | ✗ |\n| R        | .range          | R(['a', 'z'])         | ``[a-z]              ``    | ✓ | ✓ | ✓ |\n| RS       | .ranges         | RS(['a', 'z'])        | ``[a-z]+             ``    | ✓ | ✓ | ✗ |\n| ST       | .something      | ST()                  | ``(?:.+)             ``    | ✗ | ✓ | ✗ |\n| STB      | .something_but  | STB('0258qaz')        | ``(?:[^0258qaz]+)    ``    | ✓ | ✓ | ✗ |\n| SOL      | .start_of_line  | SOL()                 | ``^                  ``    | ✗ | ✗ | ✗ |\n| BR       | .br             | BR()                  | ``(?:\\n``\u0026#124;``\\r\\n)``   | ✗ | ✗ | ✗ |\n| D        | .digit          | D()                   | ``\\d                 ``    | ✗ | ✓ | ✓ |\n| DS       | .digits         | DS()                  | ``\\d+                ``    | ✗ | ✓ | ✗ |\n| DS       | .int_or_decimal | ID()                  | ``(?:\\d*\\.)?\\d+      ``    | ✗ | ✓ | ✗ |\n| ND       | .non_digit      | ND()                  | ``\\D                 ``    | ✗ | ✓ | ✓ |\n| NDS      | .non_digits     | NDS()                 | ``\\D+                ``    | ✗ | ✓ | ✗ |\n| TAB      | .tab            | TAB()                 | ``\\t                 ``    | ✗ | ✗ | ✓ |\n| WS       | .whitespace     | WS()                  | ``\\s                 ``    | ✗ | ✗ | ✓ |\n| NWS      | .non_whitespace | NWS()                 | ``\\S                 ``    | ✗ | ✗ | ✓ |\n| W        | .word           | W()                   | ``\\w+                ``    | ✗ | ✓ | ✗ |\n| NW       | .non_word       | NW()                  | ``\\W+                ``    | ✗ | ✓ | ✗ |\n| C        | .char           | C()                   | ``\\w                 ``    | ✗ | ✓ | ✓ |\n| NC       | .non_char       | NC()                  | ``\\W                 ``    | ✗ | ✓ | ✓ |\n| FS       | .dotall/.S      | FS()                  | Flag dotall enabled        | ✗ | ✗ | ✗ |\n| FI       | .ignorecase/.I  | FI()                  | Flag ignorecase enabled    | ✗ | ✗ | ✗ |\n| FL       | .locale/.L      | FL()                  | Flag locale enabled        | ✗ | ✗ | ✗ |\n| FM       | .multiline/.M   | FM()                  | Flag multiline enabled     | ✗ | ✗ | ✗ |\n| FU       | .unicode/.U     | FU()                  | Flag unicode enabled       | ✗ | ✗ | ✗ |\n| FX       | .verbose/.X     | FX()                  | Flag verbose enabled       | ✗ | ✗ | ✗ |\n\nOther methods of regex object\n\n| Method     | Description\n|------------|---------------\n| .get_flags | returns an integer with the value of the flags\n| .compile   | same as re.compile\n| .findall   | same as re.findall\n| .groups    | same as re.groups\n| .groupdict | return a match object =\u003e a defaultdict(None) Like that contains all the results of a match\n| .match     | same as re.match\n| .replace   | return the string obtained by replacing\n| .search    | same as re.search\n| .split     | same as re.split\n| .test      | returns true if the result of .findall have size greater than 0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzokis%2Fhumanregex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzokis%2Fhumanregex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzokis%2Fhumanregex/lists"}