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