Debian Codegraph @

doc/Tutorial_4.sor

raw
1 # Soros Tutorial Four 👪 Reference & Group
2 #
3 # We will write a thousand separation program
4 # by referring parts of the input text and
5 # learning more about regular expression syntax.
6 #
7 #
8 #
9 # TASK 1
10 #
11 # We can create character groups in the regex pattern by
12 # parenthesizing, and refer them using reference \1..\9
13 # in the regex replacement.
14 #
15 # For example, following program replaces the
16 # characters of arbitry 2-character input (ab -> ba, etc.):
17 #
18 # (.)(.) \2\1
19 #
20 # Write a program to return with the last two characters of the
21 # input!
22
23
24
25 # TASK 2
26 #
27 # Character sequences in bracket expression can be abbreviated
28 # using character ranges separated by a hyphen character.
29 #
30 # For example, [a-z] matches every character between “a” and “z”,
31 # [a-cx-z] matches letter a, b, c, x, y and z.
32 #
33 # Using this write a program to return with the first character
34 # of the input, if that is between 1 and 5, or 9!
35
36
37
38 # TASK 3
39 #
40 # Bracket expression [^...] matches complementers of the
41 # given characters or character ranges. For example,
42 # [^01] matches every character, except 0 or 1.
43 #
44 # Write a program to return with the last two characters
45 # of the input, if they don’t contain zeroes.
46
47
48
49 # TASK 4
50 #
51 # The pattern \d matches an arbitrary digit. For example
52 # \d\d\d matches every 3-digit numbers.
53 #
54 # Using this, write a program to return with the first
55 # three digits of the input!
56
57
58
59 # TASK 5
60 #
61 # Repetition sign + and * can be combined with bracket
62 # expressions, \d and groups, too. There are three other
63 # frequently used repetitions:
64 #
65 # ? 0, or 1 times
66 # {n} exactly n times
67 # {n,m} between n and m times
68 #
69 # For example, \d{6}, \d{1,3}
70 #
71 # Using this, write a program to do thousand separation
72 # between 1 and 100,000,000,000.
73 #
74 # Rules:
75 #
76 # I. Separation is only from 5-digit numbers.
77 # For example: 1, 10, 1000, 10,000.
78 #
79 # II. Separate by commas by 3-digit groups from the right side
80 # For example: 100,000, 1000,000, 10,000,000, 100,000,000...
81 #
82 # NOTE: Test your program by typing a single number in the textbox “Input”.
83
84
85
86 # OPTIONAL TASK 1
87 #
88 # Groups can match different patterns:
89 #
90 # (word1|word2|word3)
91 #
92 # matches word1 OR word2 OR word3.
93 #
94 # For example, ([a-z]|10*) matches a single letter OR
95 # 10 OR 100 OR 1000 etc.
96 #
97 # Groups can contain inner groups, too.
98 #
99 # Guess it, what does the following expression!
100 #
101 # ((Anne|Becky) (Smith|Johnson)) Full name: \1, First name: \2
102
103
104
105 # OPTIONAL TASK 2
106 #
107 # References can be used within the regex
108 # pattern (back references).
109 #
110 # Following program matches all 2-digit numbers
111 # with repeating digits (11, 22, 33, 44 .. 99).
112 #
113 # (\d)\1 Cute number! 🙂
114 #
115 # Extend this pattern for every numbers contain only repeating
116 # digits (11, 22, ... 111... 555)!
117
118
119
120
121
122
123
124 SCROLL DOWN FOR SOLUTIONS
125 ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩
126
127
128
129
130
131
132
133
134 # SOLUTION – TASK 1
135 #
136 # .*(..) \1
137 #
138 # or the less simple
139 #
140 # .*(.)(.) \1\2
141
142
143
144 # SOLUTION – TASK 2
145 #
146 # ([1-59]).* \1
147
148
149
150 # SOLUTION – TASK 3
151 #
152 # .*([^0][^0]) \1
153
154
155
156 # SOLUTION – TASK 4
157 #
158 # (\d\d\d).* \1
159
160
161
162 # SOLUTION – TASK 5
163 #
164 # NOTE: Last line will return with smaller and big numbers
165 # without changes:
166 #
167 # (\d{2,3})(\d{3}) \1,\2
168 # (\d{1,3})(\d{3})(\d{3}) \1,\2,\3
169 # (\d{1,3})(\d{3})(\d{3})(\d{3}) \1,\2,\3,\4
170 # .* \0
171
172
173
174 # SOLUTION – OPTIONAL TASK 1
175
176 # ((Anne|Becky) (Smith|Johnson)) Full name: \1, First name: \2
177 # expression replaces this way:
178 #
179 # Anne Smith -> Full name: Anne Smith, First name: Anne
180 # Becky Smith -> Full name: Becky Smith, First name: Becky
181 # Anne Johnson -> Full name: Anne Johnson, First name: Anne
182 # Becky Johnson -> Full name: Becky Johnson, First name: Becky
183
184
185
186 # SOLUTION – OPTIONAL TASK 2
187 #
188 # (\d)\1+ Cute number! 🙂
189
190 #######################################################
191 # CONGRATULATIONS – You have finished Tutorial Four! 🙂
192 #######################################################