ziishaned /learn-regex

Learn regex the easy way

★ 46,052⑂ 6,095👁 873⑃ 181● 87▤ 0◈ 0
MITCreated 2017-07-22gum.co/learn-regex
46,052
stars
+-2
6,095
forks
873
watchers
181
prs
87
issues
0
releases
0
discussions

README

Translations:

 English
 German
 Español
 Français
 Português do Brasil
 中文版
 日本語
 한국어
 Turkish
 Greek
 Magyar
 Polish
 Русский
 Tiếng Việt
 Bahasa Indonesia
 فارسی
 עברית


 What is Regular Expression?


    
        
    


 A regular expression is a group of characters or symbols which is used to find a specific pattern in a text.

A regular expression is a pattern that is matched against a subject string from
left to right. Regular expressions are used to replace text within a string, 
validate forms, extract a substring from a string based on a pattern match, 
and so much more. The term "regular expression" is a mouthful, so you will usually
find the term abbreviated to "regex" or "regexp". 

Imagine you are writing an application and you want to set the rules for when a
user chooses their username. We want to allow the username to contain letters,
numbers, underscores and hyphens. We also want to limit the number of characters
in the username so it does not look ugly. We can use the following regular expression to
validate the username:



  


The regular expression above can accept the strings john_doe, jo-hn_doe and
john12_as. It does not match Jo because that string contains an uppercase
letter and also it is too short.

 Table of Contents

- Basic Matchers
- Meta Characters
  - The Full Stop
  - Character Sets
    - Negated Character Sets
  - Repetitions
    - The Star
    - The Plus
    - The Question Mark
  - Braces
  - Capturing Groups
      - Non-Capturing Groups
  - Alternation
  - Escaping Special Characters
  - Anchors
    - The Caret
    - The Dollar Sign
- Shorthand Character Sets
- Lookarounds
  - Positive Lookahead
  - Negative Lookahead
  - Positive Lookbehind
  - Negative Lookbehind
- Flags
  - Case Insensitive
  - Global Search
  - Multiline
- Greedy vs Lazy Matching

 1. Basic Matchers

A regular expression is just a pattern of characters that we use to perform a
search in a text.  For example, the regular expression the means: the letter
t, followed by the letter h, followed by the letter e.


"the" = The fat cat sat on the mat.


Test the regular expression

The regular expression 123 matches the string 123. The regular expression is
matched against an input string by comparing each character in the regular
expression to each character in the input string, one after another. Regular
expressions are normally case-sensitive so the regular expression The would
not match the string the.


"The" = The fat cat sat on the mat.


Test the regular expression

 2. Meta Characters

Meta characters are the building blocks of regular expressions.  Meta
characters do not stand for themselves but instead are interpreted in some
special way. Some meta characters have a special meaning and are written inside
square brackets. The meta characters are as follows:

|Meta character|Description|
|:----:|----|
|.|Period matches any single character except a line break.|
|[ ]|Character class. Matches any character contained between the square 

Top issues

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON