|
| 1 | +''' |
| 2 | +Ram, consider this file as beginer level tutorial kinda things. |
| 3 | +you will learn about regular expressions (RegEx), and use Python's re module to work with RegEx (with the help of examples). |
| 4 | + |
| 5 | +A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. |
| 6 | + |
| 7 | +''' |
| 8 | +# below line a regex pattern |
| 9 | +example_pattern = '^a...s$' |
| 10 | + |
| 11 | +# What can we do with this above pattern? |
| 12 | +# What did you understand when you read this pattern? |
| 13 | + |
| 14 | +# Matching Patterns with example.. |
| 15 | + |
| 16 | +# Expression String Matched |
| 17 | +# ^a...s$ abs No match |
| 18 | + # alias Match |
| 19 | + # abyss Match |
| 20 | + # Alias No match |
| 21 | + # An abacus No match |
| 22 | + |
| 23 | + |
| 24 | +# Code Implementation example, |
| 25 | + |
| 26 | +import re # re is the pre-defined module that exist inside Python |
| 27 | + |
| 28 | +sample_test_string = 'Sanjay is conducting a session to Ram alias prrampalli who is residing in London' |
| 29 | +sample_focused_string = "abyss" |
| 30 | + |
| 31 | +result = re.match(example_pattern, sample_focused_string) |
| 32 | +print(result) |
| 33 | + |
| 34 | +if result: |
| 35 | + print("Search successful.") |
| 36 | +else: |
| 37 | + print("Search unsuccessful.") |
| 38 | + |
| 39 | + |
| 40 | +# from the above example, we used re.match() to check string match, there're other method under re, those are, |
| 41 | +# re.compile |
| 42 | +# re.sub |
| 43 | +# re.search |
| 44 | +# re.findall |
| 45 | +# re.split |
| 46 | + |
| 47 | +# To specify regular expressions, metacharacters are used. In the above example, ^ and $ are metacharacters |
| 48 | + |
| 49 | +''' |
| 50 | +What is meta characters? |
| 51 | + |
| 52 | +Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of metacharacters: |
| 53 | + |
| 54 | +[] . ^ $ * + ? {} () \ | |
| 55 | + |
| 56 | +''' |
| 57 | + |
| 58 | +# 1 - We can discuss about [] |
| 59 | + |
| 60 | +# Expression String Matched? |
| 61 | +# [abc] a 1 match |
| 62 | +# ac 2 matches |
| 63 | +# Hey Jude No match |
| 64 | +# abc de ca 5 matches |
| 65 | + |
| 66 | +# [abc] will match if the string you are trying to match contains any of the a, b or c. |
| 67 | + |
| 68 | +# You can also specify a range of characters using - inside square brackets. |
| 69 | + |
| 70 | +# [a-e] is the same as [abcde]. |
| 71 | +# [1-4] is the same as [1234]. |
| 72 | +# [0-39] is the same as [01239]. ** |
| 73 | + |
| 74 | +# You can complement (invert) the character set by using caret ^ symbol at the start of a square-bracket. |
| 75 | + |
| 76 | +# [^abc] means any character except a or b or c. |
| 77 | +# [^0-9] means any non-digit character. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# 2 - We can now discuss about . |
| 82 | + |
| 83 | +''' |
| 84 | +. -> called as period |
| 85 | + |
| 86 | +A period matches any single character (except newline '\n'). |
| 87 | + |
| 88 | +Expression String Matched? |
| 89 | +.. a No match |
| 90 | + ac 1 match |
| 91 | + acd 1 match |
| 92 | + acde 2 matches (contains 4 characters) |
| 93 | + |
| 94 | +''' |
| 95 | + |
| 96 | +# 3 - We can discuss about Carret. |
| 97 | + |
| 98 | +''' |
| 99 | +^ - Caret |
| 100 | + |
| 101 | +The caret symbol ^ is used to check if a string starts with a certain character. |
| 102 | + |
| 103 | +Expression String Matched? |
| 104 | +^a a 1 match |
| 105 | + abc 1 match |
| 106 | + bac No match |
| 107 | +^ab abc 1 match |
| 108 | + acb No match (starts with a but not followed by b) |
| 109 | + |
| 110 | +''' |
| 111 | + |
| 112 | +# 4 - We can look into $ |
| 113 | + |
| 114 | +''' |
| 115 | +$ - Dollar |
| 116 | + |
| 117 | +The dollar symbol $ is used to check if a string ends with a certain character. |
| 118 | + |
| 119 | +Expression String Matched? |
| 120 | +a$ a 1 match |
| 121 | + formula 1 match |
| 122 | + cab No match |
| 123 | + ram No Match |
| 124 | + ra 1 Match |
| 125 | +''' |
| 126 | + |
| 127 | + |
| 128 | +# 5 - Astriek or star |
| 129 | + |
| 130 | +''' |
| 131 | +* - Star |
| 132 | + |
| 133 | +The star symbol * matches zero or more occurrences of the pattern left to it. |
| 134 | + |
| 135 | +Expression String Matched? |
| 136 | +ma*n mn 1 match |
| 137 | + man 1 match |
| 138 | + maaan 1 match |
| 139 | + main No match (a is not followed by n) |
| 140 | + woman 1 match |
| 141 | + ran No match |
| 142 | + ram No Match |
| 143 | + maen No Match |
| 144 | + maaaaaaan 1 Match |
| 145 | +''' |
| 146 | + |
| 147 | +# 6 - Plus |
| 148 | +''' |
| 149 | ++ - Plus |
| 150 | + |
| 151 | +The plus symbol + matches one or more occurrences of the pattern left to it. |
| 152 | + |
| 153 | +Expression String Matched? |
| 154 | +ma+n mn No match (no a character) |
| 155 | + man 1 match |
| 156 | + maaan 1 match |
| 157 | + main No match (a is not followed by n) |
| 158 | + woman 1 match |
| 159 | +''' |
| 160 | + |
| 161 | +# 7 - ? - Question Mark |
| 162 | +''' |
| 163 | +The question mark symbol ? matches zero or one occurrence of the pattern left to it. |
| 164 | + |
| 165 | +Expression String Matched? |
| 166 | +ma?n mn 1 match |
| 167 | + man 1 match |
| 168 | + maaan No match (more than one a character) |
| 169 | + main No match (a is not followed by n) |
| 170 | + woman 1 match |
| 171 | + maon 1 match |
| 172 | + exmain 1 match |
| 173 | +''' |
| 174 | + |
| 175 | + |
| 176 | +# 8 - {} - Braces |
| 177 | + |
| 178 | +''' |
| 179 | +Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it. |
| 180 | + |
| 181 | +Expression String Matched? |
| 182 | +a{2,3} abc dat No match |
| 183 | + abc daat 1 match (at daat) |
| 184 | + aabc daaat 2 matches (at aabc and daaat) |
| 185 | + aabc daaaat 2 matches (at aabc and daaaat) |
| 186 | + |
| 187 | +Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more than 4 digits |
| 188 | + |
| 189 | +Expression String Matched? |
| 190 | +[0-9]{2,4} ab123csde 1 match (match at ab123csde) |
| 191 | + 12 and 345673 3 matches (12, 3456, 73) |
| 192 | + 1 and 2 No match |
| 193 | +''' |
| 194 | + |
| 195 | +# 9. | - Alternation |
| 196 | +''' |
| 197 | +Vertical bar | is used for alternation (or operator). |
| 198 | + |
| 199 | +Expression String Matched? |
| 200 | +a|b cde No match |
| 201 | + ade 1 match (match at ade) |
| 202 | + acdbea 3 matches (at acdbea) |
| 203 | + |
| 204 | +Here, a|b match any string that contains either a or b |
| 205 | +''' |
| 206 | +# 10 () - Group |
| 207 | +''' |
| 208 | +Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that matches either a or b or c followed by xz |
| 209 | + |
| 210 | +Expression String Matched? |
| 211 | +(a|b|c)xz ab xz No match |
| 212 | +abxz 1 match (match at abxz) |
| 213 | +axz cabxz 2 matches (at axzbc cabxz) |
| 214 | +''' |
| 215 | + |
| 216 | +# 11. \ - Backslash |
| 217 | +''' |
| 218 | +Backlash \ is used to escape various characters including all metacharacters. For example, |
| 219 | + |
| 220 | +\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a special way. |
| 221 | + |
| 222 | +If you are unsure if a character has special meaning or not, you can put \ in front of it. |
| 223 | +This makes sure the character is not treated in a special way. |
| 224 | + |
| 225 | +Special Sequences |
| 226 | +------------------- |
| 227 | + |
| 228 | +Special sequences make commonly used patterns easier to write. Here's a list of special sequences: |
| 229 | + |
| 230 | +\A - Matches if the specified characters are at the start of a string. |
| 231 | + |
| 232 | +Expression String Matched? |
| 233 | +\A the the sun Match |
| 234 | + In the sun No match |
| 235 | + |
| 236 | +\b - Matches if the specified characters are at the beginning or end of a word. |
| 237 | + |
| 238 | +Expression String Matched? |
| 239 | +\bfoo football Match |
| 240 | + a football Match |
| 241 | + afootball No match |
| 242 | + |
| 243 | +foo\b the foo Match |
| 244 | + the afoo test Match |
| 245 | + the afootest No match |
| 246 | + |
| 247 | +\B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a word. |
| 248 | + |
| 249 | +Expression String Matched? |
| 250 | +\Bfoo football No match |
| 251 | + a football No match |
| 252 | + afootball Match |
| 253 | + |
| 254 | +foo\B the foo No match |
| 255 | + the afoo test No match |
| 256 | + the afootest Match |
| 257 | + |
| 258 | +\d - Matches any decimal digit. Equivalent to [0-9] |
| 259 | + |
| 260 | +Expression String Matched? |
| 261 | +\d 12abc3 3 matches (at 12abc3) |
| 262 | + Python No match |
| 263 | + |
| 264 | +\D - Matches any non-decimal digit. Equivalent to [^0-9] |
| 265 | + |
| 266 | +Expression String Matched? |
| 267 | +\D 1ab34"50 3 matches (at 1ab34"50) |
| 268 | + 1345 No match |
| 269 | + |
| 270 | +\s - Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v]. |
| 271 | + |
| 272 | +Expression String Matched? |
| 273 | +\s Python RegEx 1 match |
| 274 | + PythonRegEx No match |
| 275 | + |
| 276 | +\S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\v]. |
| 277 | + |
| 278 | +Expression String Matched? |
| 279 | +\S a b 2 matches (at a b) |
| 280 | + No match |
| 281 | + |
| 282 | +\w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_]. By the way, underscore _ is also considered an alphanumeric character. |
| 283 | + |
| 284 | +Expression String Matched? |
| 285 | +\w 12&": ;c 3 matches (at 12&": ;c) |
| 286 | + %"> ! No match |
| 287 | + |
| 288 | + |
| 289 | +\W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_] |
| 290 | + |
| 291 | +Expression String Matched? |
| 292 | +\W 1a2%c 1 match (at 1a2%c) |
| 293 | + Python No match |
| 294 | + |
| 295 | +\Z - Matches if the specified characters are at the end of a string. |
| 296 | + |
| 297 | +Expression String Matched? |
| 298 | +Python\Z I like Python 1 match |
| 299 | + I like Python Programming No match |
| 300 | + Python is fun. No match |
| 301 | + |
| 302 | +Tip: To build and test regular expressions, you can use RegEx tester tools such as regex101. This tool not only helps you in creating regular expressions, but it also helps you learn it. |
| 303 | + |
| 304 | +Now you understand the basics of RegEx, let's discuss how to use RegEx in your Python code. |
| 305 | +''' |
| 306 | + |
| 307 | + |
| 308 | +# From the above features we went through, here's the list of exercise for you! |
| 309 | + |
| 310 | +# 1. Write a method(giveMeNumber) takes input as string, gives output as available numbers inside the give string. |
| 311 | + |
| 312 | +# Sample, |
| 313 | +# givenInput = "hey my phone number starts with 91 as country code, then my 10 digit # continues - 9944294841" |
| 314 | + |
| 315 | +# giveMeNumber(givenInput) #[91, 10, 9944294841] |
| 316 | + |
| 317 | + |
| 318 | +# 2. |
| 319 | + |
| 320 | + |
0 commit comments