{"id":13395243,"url":"https://github.com/ziishaned/learn-regex","last_synced_at":"2026-01-28T15:06:44.159Z","repository":{"id":37270758,"uuid":"98029592","full_name":"ziishaned/learn-regex","owner":"ziishaned","description":"Learn regex the easy way","archived":false,"fork":false,"pushed_at":"2025-08-25T23:17:54.000Z","size":928,"stargazers_count":46114,"open_issues_count":65,"forks_count":6249,"subscribers_count":890,"default_branch":"master","last_synced_at":"2025-10-19T13:07:58.855Z","etag":null,"topics":["learn-regex","regex","regular-expression"],"latest_commit_sha":null,"homepage":"https://gum.co/learn-regex","language":null,"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/ziishaned.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["ziishaned"]}},"created_at":"2017-07-22T12:21:03.000Z","updated_at":"2025-10-19T09:44:39.000Z","dependencies_parsed_at":"2024-09-13T02:54:15.324Z","dependency_job_id":"f6954856-dfe1-4c8a-a35a-0bf2c4d1ac71","html_url":"https://github.com/ziishaned/learn-regex","commit_stats":{"total_commits":248,"total_committers":106,"mean_commits":2.339622641509434,"dds":0.6532258064516129,"last_synced_commit":"22901bc1db0eb357dc5b2bdc5b2476f97b887b74"},"previous_names":["zeeshanu/learn-regex"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ziishaned/learn-regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziishaned%2Flearn-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziishaned%2Flearn-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziishaned%2Flearn-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziishaned%2Flearn-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziishaned","download_url":"https://codeload.github.com/ziishaned/learn-regex/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziishaned%2Flearn-regex/sbom","scorecard":{"id":1238971,"data":{"date":"2025-10-13","repo":{"name":"github.com/ziishaned/learn-regex","commit":"5a5252eb51fd9c49f194f73b4f85f649da96b463"},"scorecard":{"version":"v5.3.1-0.20251012233417-af419a5f4ac8","commit":"af419a5f4ac865788a844e5a2e726681cf4ff42b"},"score":4,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"1 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#maintained"}},{"name":"Code-Review","score":7,"reason":"Found 11/15 approved changesets -- score normalized to 7","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#fuzzing"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#vulnerabilities"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.md:0","Info: FSF or OSI recognized license: MIT License: LICENSE.md:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 26 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/af419a5f4ac865788a844e5a2e726681cf4ff42b/docs/checks.md#sast"}}]},"last_synced_at":"2025-10-19T21:18:56.219Z","repository_id":37270758,"created_at":"2025-10-19T21:18:56.219Z","updated_at":"2025-10-19T21:18:56.219Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28846116,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-28T13:02:32.985Z","status":"ssl_error","status_checked_at":"2026-01-28T13:02:04.945Z","response_time":57,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["learn-regex","regex","regular-expression"],"created_at":"2024-07-30T17:01:47.521Z","updated_at":"2026-01-28T15:06:44.153Z","avatar_url":"https://github.com/ziishaned.png","language":null,"readme":"\u003cp align=\"center\"\u003e\n    \u003cbr/\u003e\n    \u003ca href=\"https://github.com/ziishaned/learn-regex\"\u003e\t\n        \u003cimg src=\"https://i.imgur.com/bYwl7Vf.png\" alt=\"Learn Regex\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n## Translations:\n\n* [English](README.md)\n* [German](translations/README-de.md)\n* [Español](translations/README-es.md)\n* [Français](translations/README-fr.md)\n* [Português do Brasil](translations/README-pt_BR.md)\n* [中文版](translations/README-cn.md)\n* [日本語](translations/README-ja.md)\n* [한국어](translations/README-ko.md)\n* [Turkish](translations/README-tr.md)\n* [Greek](translations/README-gr.md)\n* [Magyar](translations/README-hu.md)\n* [Polish](translations/README-pl.md)\n* [Русский](translations/README-ru.md)\n* [Tiếng Việt](translations/README-vn.md)\n* [Bahasa Indonesia](translations/README-id.md)\n* [فارسی](translations/README-fa.md)\n* [עברית](translations/README-he.md)\n\n\n## What is Regular Expression?\n\n\u003cp\u003e\n    \u003ca href=\"https://gum.co/learn-regex\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/-Download%20PDF%20-0a0a0a.svg?style=flat\u0026colorA=0a0a0a\" alt=\"Download PDF\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003e A regular expression is a group of characters or symbols which is used to find a specific pattern in a text.\n\nA regular expression is a pattern that is matched against a subject string from\nleft to right. Regular expressions are used to replace text within a string, \nvalidate forms, extract a substring from a string based on a pattern match, \nand so much more. The term \"regular expression\" is a mouthful, so you will usually\nfind the term abbreviated to \"regex\" or \"regexp\". \n\nImagine you are writing an application and you want to set the rules for when a\nuser chooses their username. We want to allow the username to contain letters,\nnumbers, underscores and hyphens. We also want to limit the number of characters\nin the username so it does not look ugly. We can use the following regular expression to\nvalidate the username:\n\n\u003cbr/\u003e\u003cbr/\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"./img/regexp-en.png\" alt=\"Regular expression\"\u003e\n\u003c/p\u003e\n\nThe regular expression above can accept the strings `john_doe`, `jo-hn_doe` and\n`john12_as`. It does not match `Jo` because that string contains an uppercase\nletter and also it is too short.\n\n## Table of Contents\n\n- [Basic Matchers](#1-basic-matchers)\n- [Meta Characters](#2-meta-characters)\n  - [The Full Stop](#21-the-full-stop)\n  - [Character Sets](#22-character-sets)\n    - [Negated Character Sets](#221-negated-character-sets)\n  - [Repetitions](#23-repetitions)\n    - [The Star](#231-the-star)\n    - [The Plus](#232-the-plus)\n    - [The Question Mark](#233-the-question-mark)\n  - [Braces](#24-braces)\n  - [Capturing Groups](#25-capturing-groups)\n      - [Non-Capturing Groups](#251-non-capturing-groups)\n  - [Alternation](#26-alternation)\n  - [Escaping Special Characters](#27-escaping-special-characters)\n  - [Anchors](#28-anchors)\n    - [The Caret](#281-the-caret)\n    - [The Dollar Sign](#282-the-dollar-sign)\n- [Shorthand Character Sets](#3-shorthand-character-sets)\n- [Lookarounds](#4-lookarounds)\n  - [Positive Lookahead](#41-positive-lookahead)\n  - [Negative Lookahead](#42-negative-lookahead)\n  - [Positive Lookbehind](#43-positive-lookbehind)\n  - [Negative Lookbehind](#44-negative-lookbehind)\n- [Flags](#5-flags)\n  - [Case Insensitive](#51-case-insensitive)\n  - [Global Search](#52-global-search)\n  - [Multiline](#53-multiline)\n- [Greedy vs Lazy Matching](#6-greedy-vs-lazy-matching)\n\n## 1. Basic Matchers\n\nA regular expression is just a pattern of characters that we use to perform a\nsearch in a text.  For example, the regular expression `the` means: the letter\n`t`, followed by the letter `h`, followed by the letter `e`.\n\n\u003cpre\u003e\n\"the\" =\u003e The fat cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/dmRygT/1)\n\nThe regular expression `123` matches the string `123`. The regular expression is\nmatched against an input string by comparing each character in the regular\nexpression to each character in the input string, one after another. Regular\nexpressions are normally case-sensitive so the regular expression `The` would\nnot match the string `the`.\n\n\u003cpre\u003e\n\"The\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on the mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/1paXsy/1)\n\n## 2. Meta Characters\n\nMeta characters are the building blocks of regular expressions.  Meta\ncharacters do not stand for themselves but instead are interpreted in some\nspecial way. Some meta characters have a special meaning and are written inside\nsquare brackets. The meta characters are as follows:\n\n|Meta character|Description|\n|:----:|----|\n|.|Period matches any single character except a line break.|\n|[ ]|Character class. Matches any character contained between the square brackets.|\n|[^ ]|Negated character class. Matches any character that is not contained between the square brackets|\n|*|Matches 0 or more repetitions of the preceding symbol.|\n|+|Matches 1 or more repetitions of the preceding symbol.|\n|?|Makes the preceding symbol optional.|\n|{n,m}|Braces. Matches at least \"n\" but not more than \"m\" repetitions of the preceding symbol.|\n|(xyz)|Character group. Matches the characters xyz in that exact order.|\n|\u0026#124;|Alternation. Matches either the characters before or the characters after the symbol.|\n|\u0026#92;|Escapes the next character. This allows you to match reserved characters \u003ccode\u003e[ ] ( ) { } . * + ? ^ $ \\ \u0026#124;\u003c/code\u003e|\n|^|Matches the beginning of the input.|\n|$|Matches the end of the input.|\n\n## 2.1 The Full Stop\n\nThe full stop `.` is the simplest example of a meta character. The meta character `.`\nmatches any single character. It will not match return or newline characters.\nFor example, the regular expression `.ar` means: any character, followed by the\nletter `a`, followed by the letter `r`.\n\n\u003cpre\u003e\n\".ar\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/xc9GkU/1)\n\n## 2.2 Character Sets\n\nCharacter sets are also called character classes. Square brackets are used to\nspecify character sets. Use a hyphen inside a character set to specify the\ncharacters' range. The order of the character range inside the square brackets\ndoesn't matter. For example, the regular expression `[Tt]he` means: an uppercase\n`T` or lowercase `t`, followed by the letter `h`, followed by the letter `e`.\n\n\u003cpre\u003e\n\"[Tt]he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/2ITLQ4/1)\n\nA period inside a character set, however, means a literal period. The regular\nexpression `ar[.]` means: a lowercase character `a`, followed by the letter `r`,\nfollowed by a period `.` character.\n\n\u003cpre\u003e\n\"ar[.]\" =\u003e A garage is a good place to park a c\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ear.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/wL3xtE/1)\n\n### 2.2.1 Negated Character Sets\n\nIn general, the caret symbol represents the start of the string, but when it is\ntyped after the opening square bracket it negates the character set. For\nexample, the regular expression `[^c]ar` means: any character except `c`,\nfollowed by the character `a`, followed by the letter `r`.\n\n\u003cpre\u003e\n\"[^c]ar\" =\u003e The car \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/nNNlq3/1)\n\n## 2.3 Repetitions\n\nThe meta characters `+`, `*` or `?` are used to specify how many times a\nsubpattern can occur. These meta characters act differently in different\nsituations.\n\n### 2.3.1 The Star\n\nThe `*` symbol matches zero or more repetitions of the preceding matcher. The\nregular expression `a*` means: zero or more repetitions of the preceding lowercase\ncharacter `a`. But if it appears after a character set or class then it finds\nthe repetitions of the whole character set. For example, the regular expression\n`[a-z]*` means: any number of lowercase letters in a row.\n\n\u003cpre\u003e\n\"[a-z]*\" =\u003e T\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ehe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eparked\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ein\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egarage\u003c/strong\u003e\u003c/a\u003e #21.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/7m8me5/1)\n\nThe `*` symbol can be used with the meta character `.` to match any string of\ncharacters `.*`. The `*` symbol can be used with the whitespace character `\\s`\nto match a string of whitespace characters. For example, the expression\n`\\s*cat\\s*` means: zero or more spaces, followed by a lowercase `c`,\nfollowed by a lowercase `a`, followed by a lowercase `t`,\nfollowed by zero or more spaces.\n\n\u003cpre\u003e\n\"\\s*cat\\s*\" =\u003e The fat\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e cat \u003c/strong\u003e\u003c/a\u003esat on the con\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003eenation.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/gGrwuz/1)\n\n### 2.3.2 The Plus\n\nThe `+` symbol matches one or more repetitions of the preceding character. For\nexample, the regular expression `c.+t` means: a lowercase `c`, followed by\nat least one character, followed by a lowercase `t`. It needs to be\nclarified that`t` is the last `t` in the sentence.\n\n\u003cpre\u003e\n\"c.+t\" =\u003e The fat \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat sat on the mat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/Dzf9Aa/1)\n\n### 2.3.3 The Question Mark\n\nIn regular expressions, the meta character `?` makes the preceding character\noptional. This symbol matches zero or one instance of the preceding character.\nFor example, the regular expression `[T]?he` means: Optional uppercase\n`T`, followed by a lowercase `h`, followed by a lowercase `e`.\n\n\u003cpre\u003e\n\"[T]he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in the garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/cIg9zm/1)\n\n\u003cpre\u003e\n\"[T]?he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in t\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ehe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/kPpO2x/1)\n\n## 2.4 Braces\n\nIn regular expressions, braces (also called quantifiers) are used to\nspecify the number of times that a character or a group of characters can be\nrepeated. For example, the regular expression `[0-9]{2,3}` means: Match at least\n2 digits, but not more than 3, ranging from 0 to 9.\n\n\u003cpre\u003e\n\"[0-9]{2,3}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e999\u003c/strong\u003e\u003c/a\u003e7 but we rounded it off to \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e10\u003c/strong\u003e\u003c/a\u003e.0.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/juM86s/1)\n\nWe can leave out the second number. For example, the regular expression\n`[0-9]{2,}` means: Match 2 or more digits. If we also remove the comma, the\nregular expression `[0-9]{3}` means: Match exactly 3 digits.\n\n\u003cpre\u003e\n\"[0-9]{2,}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e9997\u003c/strong\u003e\u003c/a\u003e but we rounded it off to \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e10\u003c/strong\u003e\u003c/a\u003e.0.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/Gdy4w5/1)\n\n\u003cpre\u003e\n\"[0-9]{3}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e999\u003c/strong\u003e\u003c/a\u003e7 but we rounded it off to 10.0.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/Sivu30/1)\n\n## 2.5 Capturing Groups\n\nA capturing group is a group of subpatterns that is written inside parentheses \n`(...)`. As discussed before, in regular expressions, if we put a quantifier \nafter a character then it will repeat the preceding character. But if we put a quantifier\nafter a capturing group then it repeats the whole capturing group. For example,\nthe regular expression `(ab)*` matches zero or more repetitions of the character\n\"ab\". We can also use the alternation `|` meta character inside a capturing group.\nFor example, the regular expression `(c|g|p)ar` means: a lowercase `c`,\n`g` or `p`, followed by `a`, followed by `r`.\n\n\u003cpre\u003e\n\"(c|g|p)ar\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e is \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/tUxrBG/1)\n\nNote that capturing groups do not only match, but also capture, the characters for use in \nthe parent language. The parent language could be Python or JavaScript or virtually any\nlanguage that implements regular expressions in a function definition.\n\n### 2.5.1 Non-Capturing Groups\n\nA non-capturing group is a capturing group that matches the characters but \ndoes not capture the group. A non-capturing group is denoted by a `?` followed by a `:` \nwithin parentheses `(...)`. For example, the regular expression `(?:c|g|p)ar` is similar to \n`(c|g|p)ar` in that it matches the same characters but will not create a capture group.\n\n\u003cpre\u003e\n\"(?:c|g|p)ar\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e is \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/Rm7Me8/1)\n\nNon-capturing groups can come in handy when used in find-and-replace functionality or \nwhen mixed with capturing groups to keep the overview when producing any other kind of output. \nSee also [4. Lookaround](#4-lookaround).\n\n## 2.6 Alternation\n\nIn a regular expression, the vertical bar `|` is used to define alternation.\nAlternation is like an OR statement between multiple expressions. Now, you may be\nthinking that character sets and alternation work the same way. But the big\ndifference between character sets and alternation is that character sets work at the\ncharacter level but alternation works at the expression level. For example, the\nregular expression `(T|t)he|car` means: either (an uppercase `T` or a lowercase\n`t`, followed by a lowercase `h`, followed by a lowercase `e`) OR\n(a lowercase `c`, followed by a lowercase `a`, followed by\na lowercase `r`). Note that I included the parentheses for clarity, to show that either expression\nin parentheses can be met and it will match.\n\n\u003cpre\u003e\n\"(T|t)he|car\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e is parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/fBXyX0/1)\n\n## 2.7 Escaping Special Characters\n\nA backslash `\\` is used in regular expressions to escape the next character. This\nallows us to include reserved characters such as `{ } [ ] / \\ + * . $ ^ | ?` as matching characters. To use one of these special character as a matching character, prepend it with `\\`.\n\nFor example, the regular expression `.` is used to match any character except a\nnewline. Now, to match `.` in an input string, the regular expression\n`(f|c|m)at\\.?` means: a lowercase `f`, `c` or `m`, followed by a lowercase\n`a`, followed by a lowercase `t`, followed by an optional `.`\ncharacter.\n\n\u003cpre\u003e\n\"(f|c|m)at\\.?\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e sat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/DOc5Nu/1)\n\n## 2.8 Anchors\n\nIn regular expressions, we use anchors to check if the matching symbol is the\nstarting symbol or ending symbol of the input string. Anchors are of two types:\nThe first type is the caret `^` that checks if the matching character is the first\ncharacter of the input and the second type is the dollar sign `$` which checks if a matching\ncharacter is the last character of the input string.\n\n### 2.8.1 The Caret\n\nThe caret symbol `^` is used to check if a matching character is the first character\nof the input string. If we apply the following regular expression `^a` (meaning 'a' must be\nthe starting character) to the string `abc`, it will match `a`. But if we apply\nthe regular expression `^b` to the above string, it will not match anything.\nBecause in the string `abc`, the \"b\" is not the starting character. Let's take a look\nat another regular expression `^(T|t)he` which means: an uppercase `T` or\na lowercase `t` must be the first character in the string, followed by a\nlowercase `h`, followed by a lowercase `e`.\n\n\u003cpre\u003e\n\"(T|t)he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/5ljjgB/1)\n\n\u003cpre\u003e\n\"^(T|t)he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in the garage.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/jXrKne/1)\n\n### 2.8.2 The Dollar Sign\n\nThe dollar sign `$` is used to check if a matching character is the last character\nin the string. For example, the regular expression `(at\\.)$` means: a\nlowercase `a`, followed by a lowercase `t`, followed by a `.`\ncharacter and the matcher must be at the end of the string.\n\n\u003cpre\u003e\n\"(at\\.)\" =\u003e The fat c\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e s\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e on the m\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/y4Au4D/1)\n\n\u003cpre\u003e\n\"(at\\.)$\" =\u003e The fat cat. sat. on the m\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/t0AkOd/1)\n\n##  3. Shorthand Character Sets\n\nThere are a number of convenient shorthands for commonly used character sets/\nregular expressions:\n\n|Shorthand|Description|\n|:----:|----|\n|.|Any character except new line|\n|\\w|Matches alphanumeric characters: `[a-zA-Z0-9_]`|\n|\\W|Matches non-alphanumeric characters: `[^\\w]`|\n|\\d|Matches digits: `[0-9]`|\n|\\D|Matches non-digits: `[^\\d]`|\n|\\s|Matches whitespace characters: `[\\t\\n\\f\\r\\p{Z}]`|\n|\\S|Matches non-whitespace characters: `[^\\s]`|\n\n## 4. Lookarounds\n\nLookbehinds and lookaheads (also called lookarounds) are specific types of\n***non-capturing groups*** (used to match a pattern but without including it in the matching\nlist). Lookarounds are used when a pattern must be\npreceded or followed by another pattern. For example, imagine we want to get all\nnumbers that are preceded by the `$` character from the string\n`$4.44 and $10.88`. We will use the following regular expression `(?\u003c=\\$)[0-9\\.]*`\nwhich means: get all the numbers which contain the `.` character and are preceded\nby the `$` character. These are the lookarounds that are used in regular\nexpressions:\n\n|Symbol|Description|\n|:----:|----|\n|?=|Positive Lookahead|\n|?!|Negative Lookahead|\n|?\u003c=|Positive Lookbehind|\n|?\u003c!|Negative Lookbehind|\n\n### 4.1 Positive Lookahead\n\nThe positive lookahead asserts that the first part of the expression must be\nfollowed by the lookahead expression. The returned match only contains the text\nthat is matched by the first part of the expression. To define a positive\nlookahead, parentheses are used. Within those parentheses, a question mark with\nan equals sign is used like this: `(?=...)`. The lookahead expressions is written after\nthe equals sign inside parentheses. For example, the regular expression\n`(T|t)he(?=\\sfat)` means: match either a lowercase `t` or an uppercase\n `T`, followed by the letter `h`, followed by the letter `e`. In parentheses we\ndefine a positive lookahead which tells the regular expression engine to match `The`\nor `the` only if it's followed by the word `fat`.\n\n\u003cpre\u003e\n\"(T|t)he(?=\\sfat)\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on the mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/IDDARt/1)\n\n### 4.2 Negative Lookahead\n\nNegative lookaheads are used when we need to get all matches from an input string\nthat are not followed by a certain pattern. A negative lookahead is written the same way as a\npositive lookahead. The only difference is, instead of an equals sign `=`, we\nuse an exclamation mark `!` to indicate negation i.e. `(?!...)`. Let's take a look at the following\nregular expression `(T|t)he(?!\\sfat)` which means: get all `The` or `the` words\nfrom the input string that are not followed by a space character and the word `fat`.\n\n\u003cpre\u003e\n\"(T|t)he(?!\\sfat)\" =\u003e The fat cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/V32Npg/1)\n\n### 4.3 Positive Lookbehind\n\nPositive lookbehinds are used to get all the matches that are preceded by a\nspecific pattern. Positive lookbehinds are written `(?\u003c=...)`. For example, the\nregular expression `(?\u003c=(T|t)he\\s)(fat|mat)` means: get all `fat` or `mat` words\nfrom the input string that come after the word `The` or `the`.\n\n\u003cpre\u003e\n\"(?\u003c=(T|t)he\\s)(fat|mat)\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e cat sat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/avH165/1)\n\n### 4.4 Negative Lookbehind\n\nNegative lookbehinds are used to get all the matches that are not preceded by a\nspecific pattern. Negative lookbehinds are written `(?\u003c!...)`. For example, the\nregular expression `(?\u003c!(T|t)he\\s)(cat)` means: get all `cat` words from the input\nstring that are not after the word `The` or `the`.\n\n\u003cpre\u003e\n\"(?\u0026lt;!(T|t)he\\s)(cat)\" =\u003e The cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/8Efx5G/1)\n\n## 5. Flags\n\nFlags are also called modifiers because they modify the output of a regular\nexpression. These flags can be used in any order or combination, and are an\nintegral part of the RegExp.\n\n|Flag|Description|\n|:----:|----|\n|i|Case insensitive: Match will be case-insensitive.|\n|g|Global Search: Match all instances, not just the first.|\n|m|Multiline: Anchor meta characters work on each line.|\n\n### 5.1 Case Insensitive\n\nThe `i` modifier is used to perform case-insensitive matching. For example, the\nregular expression `/The/gi` means: an uppercase `T`, followed by a lowercase\n`h`, followed by an `e`. And at the end of regular expression\nthe `i` flag tells the regular expression engine to ignore the case. As you can\nsee, we also provided `g` flag because we want to search for the pattern in the\nwhole input string.\n\n\u003cpre\u003e\n\"The\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on the mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/dpQyf9/1)\n\n\u003cpre\u003e\n\"/The/gi\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/ahfiuh/1)\n\n### 5.2 Global Search\n\nThe `g` modifier is used to perform a global match (finds all matches rather than\nstopping after the first match). For example, the regular expression`/.(at)/g`\nmeans: any character except a new line, followed by a lowercase `a`,\nfollowed by a lowercase `t`. Because we provided the `g` flag at the end of\nthe regular expression, it will now find all matches in the input string, not just the first one (which is the default behavior).\n\n\u003cpre\u003e\n\"/.(at)/\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e cat sat on the mat.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/jnk6gM/1)\n\n\u003cpre\u003e\n\"/.(at)/g\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003esat\u003c/strong\u003e\u003c/a\u003e on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/dO1nef/1)\n\n### 5.3 Multiline\n\nThe `m` modifier is used to perform a multi-line match. As we discussed earlier,\nanchors `(^, $)` are used to check if a pattern is at the beginning of the input or\nthe end. But if we want the anchors to work on each line, we use\nthe `m` flag. For example, the regular expression `/at(.)?$/gm` means: a lowercase\n`a`, followed by a lowercase `t` and, optionally, anything except\na new line. And because of the `m` flag, the regular expression engine now matches patterns\nat the end of each line in a string.\n\n\u003cpre\u003e\n\"/.at(.)?$/\" =\u003e The fat\n                cat sat\n                on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/hoGMkP/1)\n\n\u003cpre\u003e\n\"/.at(.)?$/gm\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e\n                  cat \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003esat\u003c/strong\u003e\u003c/a\u003e\n                  on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n[Test the regular expression](https://regex101.com/r/E88WE2/1)\n\n## 6. Greedy vs Lazy Matching\nBy default, a regex will perform a greedy match, which means the match will be as long as\npossible. We can use `?` to match in a lazy way, which means the match should be as short as possible.\n\n\u003cpre\u003e\n\"/(.*at)/\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe fat cat sat on the mat\u003c/strong\u003e\u003c/a\u003e. \u003c/pre\u003e\n\n\n[Test the regular expression](https://regex101.com/r/AyAdgJ/1)\n\n\u003cpre\u003e\n\"/(.*?at)/\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe fat\u003c/strong\u003e\u003c/a\u003e cat sat on the mat. \u003c/pre\u003e\n\n\n[Test the regular expression](https://regex101.com/r/AyAdgJ/2)\n\n\n## Contribution\n\n* Open a pull request with improvements\n* Discuss ideas in issues\n* Spread the word\n\n## License\n\nMIT \u0026copy; Zeeshan Ahmad\n","funding_links":["https://github.com/sponsors/ziishaned"],"categories":["Others","miscellaneous","Documentation / Guides / Exercises / Boilerplates","基础/算法","Shell","Uncategorized","others","⚡ Productivity","Useful Resources","CS相关"],"sub_categories":["Tools","Uncategorized","Resources","Unicode Character","Docker"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziishaned%2Flearn-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziishaned%2Flearn-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziishaned%2Flearn-regex/lists"}