{"id":16086700,"url":"https://github.com/astares/pharo-regex-tools","last_synced_at":"2025-03-18T06:30:42.864Z","repository":{"id":78113896,"uuid":"83546339","full_name":"astares/Pharo-Regex-Tools","owner":"astares","description":"Simple tool to verify regular expressions in Pharo","archived":false,"fork":false,"pushed_at":"2019-01-30T10:57:29.000Z","size":393,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T17:39:54.338Z","etag":null,"topics":["pharo"],"latest_commit_sha":null,"homepage":null,"language":"Smalltalk","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/astares.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":"2017-03-01T11:16:54.000Z","updated_at":"2022-07-27T08:08:56.000Z","dependencies_parsed_at":"2023-02-25T02:45:47.411Z","dependency_job_id":null,"html_url":"https://github.com/astares/Pharo-Regex-Tools","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astares%2FPharo-Regex-Tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astares%2FPharo-Regex-Tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astares%2FPharo-Regex-Tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astares%2FPharo-Regex-Tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astares","download_url":"https://codeload.github.com/astares/Pharo-Regex-Tools/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244169283,"owners_count":20409691,"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":["pharo"],"created_at":"2024-10-09T13:24:56.923Z","updated_at":"2025-03-18T06:30:42.529Z","avatar_url":"https://github.com/astares.png","language":"Smalltalk","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pharo-Regex-Tools\nSimple tool to verify matches while developing with regular expressions in Pharo\n\n![](images/Tool.png)\n\n## Installation\n\nYou can load the tool using the following load expression:\n\n```Smalltalk\nMetacello new\n  baseline: 'RegexTools';\n  repository: 'github://astares/Pharo-Regex-Tools/repository';\n  load.\n```\n\n## Short guide\n\nOpen the **Regulare Expression Tester** from the Pharo **Tools** menu after you loaded the tool into your Pharo image. Alternatively you can evaluate\n\n```Smalltalk\nRegexTester open \n```\n\t\nto open the window.\t\n\nClick in the middle pane with the samples and add a new test sample. \n\n![](images/01_AddNew.png)\n\nA test sample is an example String that you would later like to be matched by the regular expression that you develop. It is something you would constantly get tested when writing your regular expression later in the upper pane.\n\n![](images/02_AddNew.png)\n\nSo for instance if you would like to develop a regular expression for email matching you might want to add the following two example emails:\n\n![](images/03_AddNew.png)\n\nSo far you have not written any regular expression - so both example emails could not match. Thats why they are displayed with a yellow icon.\n\nNow start to enter your regular expression: just type in an **[** character into the edit field in the upper pane (which is a playground for the regular expression formula). \n\nWhile typing you will notice that the samples and the expression are displayed in red as **[** is an invalid regular expression and could not yet be parsed:\n\n![](images/04_InvalidExpression.png)\n\nYou can now develop your regular expression. The first naive approach to find a regular expression that is able to handle our emails could be that we separate the expression at the **@** character. Before the **@** character there can be any word. For the time being we say that we accept the samples when after the @ sign of the email there is a \"def.com\" domain.\n\nThe way to express this with a regular expression is like this:\n\n\t[\\w]+@def.com\n\t\nAs we can see now only the first sample is accepted: \n\n![](images/05_FirstPart.png)\n\nAs you see now the first test case turns green as with the given regular expression it would be possible to parse the sample. Let's verify that our first part works by giving another sample email address (foo@def.com) with the same domain to the system:\n\n![](images/06_AnotherOne.png)\n\nAll samples that work will be displayed in **green** - all samples that wont work will be displayed in **yellow**. If your expression is invalid anything will be displayed in **red** again.\n\nThis should give enough room to experimenting further.\n\nAccepting only email from \"def.com\" domain only would not be very useful. So we would like to adopt also the last part of our expression. The domain part \"def.com\" or \"pharo.org\" can be seen as\n \n- a word\n- followed by a dot \n- followed by another word (representing the top level domain)\n\nLets extend our regular expression accordingly\n\n\t[\\w]+@[\\w]+\\.[\\w]+\n\t\nNow all the samples from this little tutorial are accepted. \n\n![](images/07_Green.png)\n\nYou can use the regular expression now in a Pharo method:\n\n```Smalltalk\nisValidEmail: aString\n\t\"Returns true if the given String represents a valid email\"\n\n\t|rx|\n\trx := '[\\w]+@[\\w]+\\.[\\w]+' asRegex.\n\t^rx matches: aString\n```\n\n\u003eSide note: Now our regular expression fits the purpose of the demonstration of the tool. If you like you can extend the expression even more. \n\n\t\n## Extended usage with subexpressions\n\nSometimes you are not only interested if a given string can be matched - but you would also be interested in the subparts that matched.\n\nSo in our simplified example we might be interested in the username of the domain name or the domain extension that was used.\n\nFor this we can divide our regular expression \n\n\t[\\w]+@[\\w]+\\.[\\w]+\n\ninto smaller subexpression by using brackets. For instance we can have a pair of brackets before the @ sign to represent the **username part** of the mail and a pair of brackets after the @ sign to represent the whole **domain part**.\n\n\t([\\w]+)@([\\w]+\\.[\\w]+)\n\nThere is nothing changed except when you click on one of the first sample \"abc@def.com\" you will notice in the lower pane of the tool the results the subexpressions are shown additionally:\n\n![](images/08_Subexpression.png)\n\nAt the first index 1 we find the full match. At the second index 2 we find the username part (the first group) and in the third index we find the domain part which can be accessed with 3.\n\nWith this knowledge we can easily write a method on our MailChecker utility class that returns the domain part of an email address by letting the regular expression to the work for us:\n\n```Smalltalk\ndomainPartFromEmail: aString\n\t\"Returns the domain part of an email\"\n\n\t|rx|\n\trx := '([\\w]+)@([\\w]+\\.[\\w]+)' asRegex.\n\n\t(rx matches: aString)\n\t\tifFalse: [ self error: 'not a valid email given' ].\n\n\t^rx subexpression: 3\n```\n\nIf you would run\n\n```Smalltalk\nMailChecker domainPartFromEmail: 'abc@def.com'\n```\n\t\nafterwards it would correctly return \"def.com\" as result.\n\nIf you like you can further subdivide your regular expression, for instance like this:\n\n\t([\\w]+)@(([\\w]+)\\.([\\w]+))\n\nto separate the **domain name** from the **domain extension** in the **domain part**\n\t\n## Scripting\n\nIf you would like to script the tool you can easily do:\n\n```Smalltalk\n| tool |\ntool := RegexTester new.\ntool expression: '([\\w]+)@(([\\w]+)\\.([\\w]+))'.\ntool matches: #('abc@def.org' 'pharo@pharo.org' 'foo@def.com' 'user@domain.org').\ntool openWithSpec\n``` \n\nThis is especially useful if you would later reopen the tool on the samples you used. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastares%2Fpharo-regex-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastares%2Fpharo-regex-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastares%2Fpharo-regex-tools/lists"}