mirror of
https://github.com/ziishaned/learn-regex.git
synced 2025-08-16 16:36:12 -04:00
Add example
This commit is contained in:
parent
5d3f9b41b3
commit
161515111d
61
README.md
61
README.md
@ -1,11 +1,21 @@
|
||||
# What is Regular Expression?
|
||||
## What is Regular Expression?
|
||||
|
||||
Regular expression is a group of character or symbols which is used to find a specific pattern from a text.
|
||||
The word "Regular expression" is a mouthful, you will usually find the term abbreviated as "regex" or
|
||||
"regexp". Regular expression is used for replacing a text withing a string, validating form, extract a substring
|
||||
from a string based upon a pattern match, and so much more.
|
||||
> Regular expression is a group of characters or symbols which is used to find a specific pattern from a text.
|
||||
|
||||
# Table of Contents
|
||||
A regular expression is a pattern that is matched against a subject string from left to right. The word "Regular expression" is a
|
||||
mouthful, you will usually find the term abbreviated as "regex" or "regexp". Regular expression is used for replacing a text withing
|
||||
a string, validating form, extract a substring from a string based upon a pattern match, and so much more.
|
||||
|
||||
Imagine you are writing an application and you want to set the rules when user chosing their username. We want the username can
|
||||
contains letter, number, underscore and hyphen. We also want to limit the number of characters in username so it does not look ugly.
|
||||
We use the following regular expression to validate a username:
|
||||
|
||||
<center></center>
|
||||
|
||||
Above regular expression can accepts the strings "john_doe", "jo-hn\_doe" and "john12\_as". It does not match "Jo" because that string
|
||||
contains uppercase letter and also it is too short.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Basic Matchers]()
|
||||
2. [Meta character]()
|
||||
@ -19,17 +29,16 @@ from a string based upon a pattern match, and so much more.
|
||||
|
||||
## 1. Basic Matchers
|
||||
|
||||
|
||||
A regular expression is just a pattern of letters and digits that we used to search in a text. For example the
|
||||
regular expression `cat` means: the letter `c`, followed by the letter `a`, followed by the letter `t`.
|
||||
A regular expression is just a pattern of letters and digits that we used to search in a text. For example the regular expression
|
||||
`cat` means: the letter `c`, followed by the letter `a`, followed by the letter `t`.
|
||||
|
||||
<pre>
|
||||
"cat" => The <strong><u>cat</u></strong> sat on the mat
|
||||
</pre>
|
||||
|
||||
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 `Cat` would not match the string "cat".
|
||||
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 `Cat` would not match the string "cat".
|
||||
|
||||
<pre>
|
||||
"Cat" => The cat sat on the <strong><u>Cat</u></strong>
|
||||
@ -37,8 +46,9 @@ Regular expressions are normally case-sensitive so the regular expression `Cat`
|
||||
|
||||
## 2. Meta Characters
|
||||
|
||||
Meta characters are the building blocks of the regular expressions. Some meta characters have a special meaning
|
||||
that are written inside the square brackets. The meta character are as follows:
|
||||
Meta characters are the building blocks of the regular expressions. Meta characters do not stand for themselves but instead are
|
||||
interpreted in some special way. Some meta characters have a special meaning that are written inside the square brackets.
|
||||
The meta character are as follows:
|
||||
|
||||
|Meta character|Description|
|
||||
|:----:|----|
|
||||
@ -57,9 +67,9 @@ that are written inside the square brackets. The meta character are as follows:
|
||||
|
||||
## 2.1 Full stop
|
||||
|
||||
Full stop `.` is the simplest example of meta character. The meta character `.` matches any single character. It
|
||||
will not match return or new line characters. For example the regular expression `.ar` means: any character,
|
||||
followed by the letter `a`, followed by the letter `r`.
|
||||
Full stop `.` is the simplest example of meta character. The meta character `.` matches any single character. It will not match return
|
||||
or new line characters. For example the regular expression `.ar` means: any character, followed by the letter `a`, followed by the
|
||||
letter `r`.
|
||||
|
||||
<pre>
|
||||
".ar" => The <strong><u>car</u></strong> <strong><u>par</u></strong>ked in the <strong><u>gar</u></strong>age.
|
||||
@ -67,10 +77,9 @@ followed by the letter `a`, followed by the letter `r`.
|
||||
|
||||
## 2.2 Character set
|
||||
|
||||
Character sets are also called character class. Square brackets are used to specify character sets. Use hyphen
|
||||
inside character set to specify the characters range. The order of the character range inside square brackets
|
||||
doesn't matter. For example the regular expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by
|
||||
the letter `h`, followed by the letter `e`.
|
||||
Character sets are also called character class. Square brackets are used to specify character sets. Use hyphen inside character set to
|
||||
specify the characters range. The order of the character range inside square brackets doesn't matter. For example the regular
|
||||
expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the letter `h`, followed by the letter `e`.
|
||||
|
||||
<pre>
|
||||
"[Tt]he" => <strong><u>The</u></strong> car parked in <strong><u>the</u></strong> garage.
|
||||
@ -78,9 +87,9 @@ the letter `h`, followed by the letter `e`.
|
||||
|
||||
### 2.2.1 Negated character set
|
||||
|
||||
In general the caret symbol represents the start of the string, but when it is typed after the opening square
|
||||
bracket it negates the character set. For example the regular expression `[^c]ar` means: any character except
|
||||
`c`, followed by the character `a`, followed by the letter `r`.
|
||||
In general the caret symbol represents the start of the string, but when it is typed after the opening square bracket it negates the
|
||||
character set. For example the regular expression `[^c]ar` means: any character except `c`, followed by the character `a`, followed by
|
||||
the letter `r`.
|
||||
|
||||
<pre>
|
||||
"[^c]ar" => The car <strong><u>par</u></strong>ked in the <strong><u>gar</u></strong>age.
|
||||
@ -89,8 +98,8 @@ bracket it negates the character set. For example the regular expression `[^c]ar
|
||||
|
||||
### 2.2.2 Repeating character set
|
||||
|
||||
We can repeat a character class by using `+`, `*` or `?` operators. For example the regular expression `[a-z]+`
|
||||
means: any number of lowercase letters in a row.
|
||||
We can repeat a character class by using `+`, `*` or `?` operators. For example the regular expression `[a-z]+` means: any number of
|
||||
lowercase letters in a row.
|
||||
|
||||
<pre>
|
||||
"[a-z]+" => <strong><u>The</u></strong> <strong><u>car</u></strong> <strong><u>parked</u></strong> <strong><u>in</u></strong> <strong><u>the</u></strong> <strong><u>garage</u></strong>.
|
||||
|
Loading…
x
Reference in New Issue
Block a user