{"id":21351874,"url":"https://github.com/hygull/try-regex","last_synced_at":"2026-05-09T18:35:52.050Z","repository":{"id":80469020,"uuid":"188529334","full_name":"hygull/try-regex","owner":"hygull","description":"A repository to work on regular expressions of different programming languages(basically Python, JavaScript, Golang etc.), trying different samples as per the need based on different requirements. ","archived":false,"fork":false,"pushed_at":"2019-06-10T18:24:51.000Z","size":132,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T17:13:41.542Z","etag":null,"topics":["golang","javascript","python","python3","regular-expression"],"latest_commit_sha":null,"homepage":"https://hygull.github.io/try-regex/","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/hygull.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-25T06:28:49.000Z","updated_at":"2019-06-10T18:24:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"6151588e-4ed6-48a7-ae1c-77779d8c3c10","html_url":"https://github.com/hygull/try-regex","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/hygull%2Ftry-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hygull","download_url":"https://codeload.github.com/hygull/try-regex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826789,"owners_count":20354220,"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":["golang","javascript","python","python3","regular-expression"],"created_at":"2024-11-22T03:11:39.925Z","updated_at":"2026-05-09T18:35:52.002Z","avatar_url":"https://github.com/hygull.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# try-regex\n\nA repository to work on regular expressions of different programming languages(basically Python, JavaScript, Golang etc.), trying different samples as per the need based on different requirements. \n\n![Relative date](https://img.shields.io/date/1558767003.svg) ![Twitter Follow](https://img.shields.io/twitter/follow/hygull.svg?color=blue\u0026logo=Rishikesh\u0026logoColor=green\u0026style=social)![Cocoapods](https://img.shields.io/cocoapods/l/AFNetworking.svg) ![Cocoapods](https://img.shields.io/cocoapods/l/AFNetworking.svg?color=green\u0026label=try-regex%20%28LICENSE%29\u0026logo=Rishikesh\u0026logoColor=red)\n\n\u003e You can also visit [https://hygull.github.io/try-regex/](https://hygull.github.io/try-regex/) to see colored beautiful documentation of this same age.\n\u003e\n\u003e **Note:** Here you can find examples from too simple to advanced as the purpose of this repository is to learn and earn knowledge step by step. So, please don't think too much if somewhere I expanded 1 line code to 2 or more.\n\n## Contents\n\n+ [Getting started - Python's regular expressions](#python-regex)\n\n+ [Getting started - JavaScript's regular expressions](#js-regex)\n\n\n## Python - List of functions/methods used in examples presented below\n\n\u003e Basic functions/methods\n\n1. **sub()**\n\n2. **compile()**\n\n3. **findall()** - a method defined on pattern object. For more details, [check this](./docs/inspect_history/compile.md).\n\n4. **match()**\n\n5. **finditer()** - a method which returns an iterator\n\n\u003e Other functions/methods\n\n1. **start()** - a method defined on match object\n\n2. **end()** -  a method defined on match object\n\n3. **span()** -  a method defined on match object\n\n\n\n\n\u003ch2 id=\"python-regex\"\u003eGetting started - Python's regular expressions\u003c/h2\u003e\n\n+ Open **Python3** terminal and list out all the packages of **re** module.\n\n```bash\nPython 3.6.7 (v3.6.7:6ec5cf24b7, Oct 20 2018, 03:02:14) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n\u003e\u003e\u003e\n\u003e\u003e\u003e import re\n\u003e\u003e\u003e \n\u003e\u003e\u003e dir(re)\n['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'RegexFlag', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_alphanum_bytes', '_alphanum_str', '_cache', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'enum', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'functools', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'template']\n\u003e\u003e\u003e \n```\n\n+ Here we don't know which are callable and which are constants/variables. Have a look at the below error by assuming that **re.A** is a callable.\n\n```python\n\u003e\u003e\u003e re.A()\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nTypeError: 'RegexFlag' object is not callable\n\u003e\u003e\u003e \n\u003e\u003e\u003e re.A\n\u003cRegexFlag.ASCII: 256\u003e\n\u003e\u003e\u003e \n```\n\n+ Finding list of all callables.\n\n\u003e Here, in the below example you can see **re.A** is not a callable. \n\n```python\n\u003e\u003e\u003e for item in dir(re):\n...     if callable(getattr(re, item)):\n...         print(item)\n... \nRegexFlag\nScanner\n_compile\n_compile_repl\n_expand\n_pattern_type\n_pickle\n_subx\ncompile\nerror\nescape\nfindall\nfinditer\nfullmatch\nmatch\npurge\nsearch\nsplit\nsub\nsubn\ntemplate\n\u003e\u003e\u003e \n```\n\n+ Retrieving all integers from text - Using **sub()** (replacement) function\n\n```python\n\u003e\u003e\u003e import re\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\n\u003e\u003e\u003e nums = re.sub(r\"\\D+\", '', text)\n\u003e\u003e\u003e nums\n'2810020090'\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = re.sub(r\"\\D+\", ' ', text)  # PERFECT\n\u003e\u003e\u003e nums\n' 2 8 100 200 90 '\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = nums.strip()  # REMOVE spaces around\n\u003e\u003e\u003e nums\n'2 8 100 200 90'\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = nums.split()\n\u003e\u003e\u003e nums\n['2', '8', '100', '200', '90']\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = map(int, nums)\n\u003e\u003e\u003e nums\n\u003cmap object at 0x103a45160\u003e\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = list(nums)\n\u003e\u003e\u003e nums\n[2, 8, 100, 200, 90]\n\u003e\u003e\u003e \n\u003e\u003e\u003e sum(nums) # SUM OF INTEGERS\n400\n\u003e\u003e\u003e \n```\n\n+ Retrieving all integers from text - Using **compile()** function \u0026 **findall()** method\n\n```python\n\u003e\u003e\u003e text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e p = re.compile(r\"\\d+\")\n\u003e\u003e\u003e \n\u003e\u003e\u003e p.findall(text)\n['2', '8', '100', '200', '90']\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = [int(num) for num in  p.findall(text)]\n\u003e\u003e\u003e nums\n[2, 8, 100, 200, 90]\n\u003e\u003e\u003e \n```\n\n+ Retrieving all integers from text - Using **compile()** function \u0026 **finditer()** method\n\n```python\n\u003e\u003e\u003e text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e p = re.compile(r\"\\d+\")\n\u003e\u003e\u003e \n\u003e\u003e\u003e iterator = p.finditer(text)\n\u003e\u003e\u003e \n\u003e\u003e\u003e match1 = iterator.__next__()\n\u003e\u003e\u003e match1.span()\n(6, 7)\n\u003e\u003e\u003e text[6: 7]\n'2'\n\u003e\u003e\u003e \n\u003e\u003e\u003e match2 = iterator.__next__()\n\u003e\u003e\u003e match2.span()\n(24, 25)\n\u003e\u003e\u003e text[24: 25]\n'8'\n\u003e\u003e\u003e \n\u003e\u003e\u003e match3 = iterator.__next__()\n\u003e\u003e\u003e match3.span()\n(40, 43)\n\u003e\u003e\u003e\n\u003e\u003e\u003e text[40: 43]\n'100'\n\u003e\u003e\u003e \n\u003e\u003e\u003e match4 = iterator.__next__()\n\u003e\u003e\u003e match4.span()\n(59, 62)\n\u003e\u003e\u003e text[59: 62]\n'200'\n\u003e\u003e\u003e \n\u003e\u003e\u003e match5 = iterator.__next__()\n\u003e\u003e\u003e match5.span()\n(69, 71)\n\u003e\u003e\u003e text[69: 71]\n'90'\n\u003e\u003e\u003e \n\u003e\u003e\u003e match6 = iterator.__next__()  # Exception\nTraceback (most recent call last):\n  File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\nStopIteration\n\u003e\u003e\u003e \n```\n\n```python\n\u003e\u003e\u003e match1.start()\n6\n\u003e\u003e\u003e match1.end()\n7\n\u003e\u003e\u003e match2.end()\n25\n\u003e\u003e\u003e match2.start()\n24\n\u003e\u003e\u003e \n```\n\nLet's use above in better way.\n\n```python\n\u003e\u003e\u003e text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e p = re.compile(r\"\\d+\")\n\u003e\u003e\u003e \n\u003e\u003e\u003e iterator = p.finditer(text)\n\u003e\u003e\u003e \n\u003e\u003e\u003e nums = []\n\u003e\u003e\u003e \n\u003e\u003e\u003e for match in iterator:\n...     start, end = match.start(), match.end()\n...     nums.append(int(text[start: end]))\n... \n\u003e\u003e\u003e nums\n[2, 8, 100, 200, 90]\n\u003e\u003e\u003e \n```\n\n+ Removing multiple underscores to get Python like variable names\n\n```python\n\u003e\u003e\u003e import re\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"full____name_\"\n\u003e\u003e\u003e output = re.sub(r\"_+\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output\n'full_name'\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"___is__to__old___\"\n\u003e\u003e\u003e output = re.sub(r\"_+\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output\n'is_to_old'\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"___iS__Too__OLD\"\n\u003e\u003e\u003e output = re.sub(r\"_+\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output\n'is_too_old'\n\u003e\u003e\u003e \n```\n\n\u003e Better way\n\n`re.sub(r\"_{2,}\", '_', text.strip(\"_\")).lower()`\n\n```python\n\u003e\u003e\u003e text = \"Python_____is__really___great__for__all__\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e output = re.sub(r\"_+\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output\n'python_is_really_great_for_all'\n\u003e\u003e\u003e \n\u003e\u003e\u003e output2 = re.sub(r\"_{2,}\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output2\n'python_is_really_great_for_all'\n\u003e\u003e\u003e \n\u003e\u003e\u003e output3 = re.sub(r\"_{3,}\", '_', text.strip(\"_\")).lower()\n\u003e\u003e\u003e output3\n'python_is__really_great__for__all'\n\u003e\u003e\u003e \n```\n\n+ Showing only words from any sentence (removing all numbers, limiting 2+ spaces to 1)\n\n```python\n\u003e\u003e\u003e import re\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"12 coins were on 5 tables for 7 days.\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e arr = re.split(r\"\\d+\", text)\n\u003e\u003e\u003e arr\n['', ' coins were on ', ' tables for ', ' days.']\n\u003e\u003e\u003e \n\u003e\u003e\u003e ''.join(arr)\n' coins were on  tables for  days.'\n\u003e\u003e\u003e \n\u003e\u003e\u003e ''.join(arr).strip()\n'coins were on  tables for  days.'\n\u003e\u003e\u003e \n\u003e\u003e\u003e re.sub(r'\\s+', ' ', ''.join(arr).strip())\n'coins were on tables for days.'\n\u003e\u003e\u003e \n```\n\n+ A simple camel case conversion to underscored case\n\n```python\n\u003e\u003e\u003e import re\n\u003e\u003e\u003e \n\u003e\u003e\u003e text = \"MyFullName\"\n\u003e\u003e\u003e \n\u003e\u003e\u003e p = re.compile(\"[A-Z]\")\n\u003e\u003e\u003e\n\u003e\u003e\u003e iterator = p.finditer(text)\n\u003e\u003e\u003e i = 0\n\u003e\u003e\u003e output = ''\n\u003e\u003e\u003e for match in iterator:\n...     if i == 0:\n...         start, end = match.start(), match.end()\n...     else:\n...         start2, end2 = match.start(), match.end()\n...         output += text[start: start2].lower() + \":\"\n...         start = start2\n...     i += 1\n... else:\n...     output += text[start2:].lower()\n... \n\u003e\u003e\u003e output\n'my:full:name'\n\u003e\u003e\u003e \n\u003e\u003e\u003e re.sub(r':', '_', output)\n'my_full_name'\n\u003e\u003e\u003e \n```\n\n\u003c!-- Node/JavaScript PART--\u003e\n\u003ch2 id=\"js-regex\"\u003eGetting started - JavaScript's regular expressions\u003c/h2\u003e\n\n+ Retrieving all integers from text (use of `split()`, `shift()`, `pop()`, `map()` methods)\n\n```javascript\n➜  try-regex git:(master) ✗ node                     \n\u003e \n\u003e let text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\nundefined\n\u003e \n\u003e let nums = text.split(/\\D+/g)\nundefined\n\u003e \n\u003e nums\n[ '', '2', '8', '100', '200', '90', '' ]\n\u003e \n\u003e if(nums.length) {\n...     if(!nums[0]) \n...         nums.shift()  // Remove first item from nums i.e. ''  \n... \n... }\n''\n\u003e \n\u003e nums\n[ '2', '8', '100', '200', '90', '' ]\n\u003e \n\u003e if(nums.length) {\n...     if(!nums[nums.length - 1]) \n...         nums.pop()  // Remove last item from nums i.e. ''  \n... }\n''\n\u003e nums\n[ '2', '8', '100', '200', '90' ]\n\u003e \n\u003e // Converting to integers (using map() method)\nundefined\n\u003e nums = nums.map((num) =\u003e Number.parseInt(num))\n[ 2, 8, 100, 200, 90 ]\n\u003e \n\u003e nums\n[ 2, 8, 100, 200, 90 ]\n\u003e \n```\n\n\u003e Attached screenshot\n\n![Node-RegexScreenShot-2019-05-25-12.57.16-PM.png](./images/Node-RegexScreenShot-2019-05-25-12.57.16-PM.png)\n\n+ Retrieving integers from text (use of `split()`, `trim()`, `replace()`, `map()` methods)\n\n```javascript\n\u003e let text = \"These 2 days, I will do 8 imp tasks for 100 years to make $200 using 90 techniques.\"\nundefined\n\u003e \n\u003e text.replace(/\\D+/g, ' ')\n' 2 8 100 200 90 '\n\u003e \n\u003e text.replace(/\\D+/g, ' ').trim() \n'2 8 100 200 90'\n\u003e \n\u003e text.replace(/\\D+/g, ' ').trim().split(' ')\n[ '2', '8', '100', '200', '90' ]\n\u003e \n\u003e text.replace(/\\D+/g, ' ').trim().split(' ').map((num) =\u003e Number.parseInt(num))\n[ 2, 8, 100, 200, 90 ]\n\u003e \n```\n\n## Python references\n\n+ [https://www.tutorialspoint.com/python3/python_reg_expressions.htm](https://www.tutorialspoint.com/python3/python_reg_expressions.htm)\n\n+ [https://docs.python.org/3/howto/regex.html](https://docs.python.org/3/howto/regex.html)\n\n## JavaScript references\n\n+ [https://www.w3schools.com/jsref/jsref_shift.asp](https://www.w3schools.com/jsref/jsref_shift.asp)\n\n+ [https://www.w3schools.com/jsref/jsref_pop.asp](https://www.w3schools.com/jsref/jsref_pop.asp)\n\n+ [https://www.w3schools.com/jsref/jsref_obj_regexp.asp](https://www.w3schools.com/jsref/jsref_obj_regexp.asp)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Ftry-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhygull%2Ftry-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Ftry-regex/lists"}