# 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! πŸ™‚ #######################################################