Skip to content

Commit dca124f

Browse files
committed
Add repetitions meta character
1 parent e24dbe5 commit dca124f

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

README.md

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ The meta character are as follows:
5454

5555
|Meta character|Description|
5656
|:----:|----|
57-
|<b>.</b>|Period matches any single character except a line break.|
58-
|<b>[ ]</b>|Character class. Matches any character contained between the square brackets.|
59-
|<b>[^ ]</b>|Negated character class. Matches any character that is not contained between the square brackets|
60-
|<b>*</b>|Matches 0 or more repetitions of the preceding symbol.|
61-
|<b>+</b>|Matches 1 or more repetitions of the preceding symbol.
62-
|<b>?</b>|Makes the preceding symbol optional.|
63-
|<b>{n}</b>|Braces. Matches “n” repetitions of the preceding symbol.|
64-
|<b>(xyz)</b>|Character group. Matches the characters xyz in that exact order.|
65-
|<b>&#124;</b>|Alternation. Matches either the characters before or the characters after the symbol.|
66-
|<b>&#92;</b>|Escapes the next character. This allows you to match reserved characters <code>[ ] ( ) { } . * + ? ^ $ \ &#124;</code>|
67-
|<b>^</b>|Matches the beginning of the input.|
68-
|<b>$</b>|Matches the end of the input.|
57+
|.|Period matches any single character except a line break.|
58+
|[ ]|Character class. Matches any character contained between the square brackets.|
59+
|[^ ]|Negated character class. Matches any character that is not contained between the square brackets|
60+
|*|Matches 0 or more repetitions of the preceding symbol.|
61+
|+|Matches 1 or more repetitions of the preceding symbol.
62+
|?|Makes the preceding symbol optional.|
63+
|{n}|Braces. Matches “n” repetitions of the preceding symbol.|
64+
|(xyz)|Character group. Matches the characters xyz in that exact order.|
65+
|&#124;|Alternation. Matches either the characters before or the characters after the symbol.|
66+
|&#92;|Escapes the next character. This allows you to match reserved characters <code>[ ] ( ) { } . * + ? ^ $ \ &#124;</code>|
67+
|^|Matches the beginning of the input.|
68+
|$|Matches the end of the input.|
6969

7070
## 2.1 Full stop
7171

@@ -87,6 +87,12 @@ expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the le
8787
"[Tt]he" => <strong><u>The</u></strong> car parked in <strong><u>the</u></strong> garage.
8888
</pre>
8989

90+
Just like above example the regular expression `ar[.]` means: an lowercase character `a`, followed by letter `r`, followed by any character.
91+
92+
<pre>
93+
"ar[.]" => The car p<strong><u>ark</u></strong>ed in the g<strong><u>ara</u></strong>ge.
94+
</pre>
95+
9096
### 2.2.1 Negated character set
9197

9298
In general the caret symbol represents the start of the string, but when it is typed after the opening square bracket it negates the
@@ -98,11 +104,49 @@ the letter `r`.
98104
</pre>
99105

100106

101-
### 2.2.2 Repeating character set
107+
## 2.3 Repetitions
108+
109+
Following meta characters `+`, `*` or `?` are used to specify how many times a subpattern can occurs. These meta characters act
110+
differently in different situations.
111+
112+
### 2.3.1 The Star
102113

103-
We can repeat a character class by using `+`, `*` or `?` operators. For example the regular expression `[a-z]+` means: any number of
104-
lowercase letters in a row.
114+
The symbol `*` matches zero or more repetitions of the preceding matcher. The regular expression `a*` means: zero or more repetitions
115+
of preceding lowercase character `a`. But if it apperas after a character set or class that it finds the repetitions of the whole
116+
character set. For example the regular expression `[a-z]*` means: any number of lowercase letters in a row.
105117

106118
<pre>
107-
"[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>.
119+
"[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> #21.
108120
</pre>
121+
122+
The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the
123+
whitespace character `\s` to match a string of whitespace characters. For example the expression `\s*cat\s*` means: zero or more
124+
spaces, followed by lowercase character `c`, followed by lowercase character `a`, followed by lowercase character `t`, followed by
125+
zero or more spaces.
126+
127+
<pre>
128+
"\s*cat\s*" => The fat<strong><u> cat </u></strong>sat on the <strong><u>cat</u></strong>.
129+
</pre>
130+
131+
### 2.3.2 The Plus
132+
133+
The symbol `+` matches one or more repetitions of the preceding character. For example the regular expression `c.+t` means: lowercase
134+
letter `c`, followed by any number of character, followed by the lowercase character `t`.
135+
136+
<pre>
137+
"c.+t" => The fat <strong><u>cat sat on the mat</u></strong>.
138+
</pre>
139+
140+
### 2.3.3 The Question Mark
141+
142+
In regular expression the meta character `?` makes the preceding character optional. This symbol matches zero or more repetitions of
143+
the preceding character. For example the regular expression `[T]?he` means: Optional the uppercase letter `T`, followed by the lowercase
144+
character `h`, followed by the lowercase character `e`.
145+
146+
<pre>
147+
"[T]he" => <strong><u>The</u></strong> car is parked in the garage.
148+
</pre>
149+
<pre>
150+
"[T]?he" => <strong><u>The</u></strong> car is parked in t<strong><u>he</u></strong> garage.
151+
</pre>
152+

0 commit comments

Comments
 (0)