@@ -230,10 +230,10 @@ False
230230
231231| Meta characters | Description |
232232| ------------- | ----------- |
233- | ` \A ` | anchors matching to beginning of string |
234- | ` \Z ` | anchors matching to end of string |
235- | ` ^ ` | anchors matching to beginning of line |
236- | ` $ ` | anchors matching to end of line |
233+ | ` \A ` | anchor to restrict matching to beginning of string |
234+ | ` \Z ` | anchor to restrict matching to end of string |
235+ | ` ^ ` | anchor to restrict matching to beginning of line |
236+ | ` $ ` | anchor to restrict matching to end of line |
237237| ` . ` | Match any character except newline character ` \n ` |
238238| | ; | OR operator for matching multiple patterns |
239239| ` (RE) ` | capturing group |
@@ -278,7 +278,7 @@ Appending a `?` to greedy quantifiers makes them non-greedy
278278| ` re.I ` | Ignore case |
279279| ` re.M ` | Multiline mode, ` ^ ` and ` $ ` anchors work on lines |
280280| ` re.S ` | Singleline mode, ` . ` will also match ` \n ` |
281- | ` re.V ` | Verbose mode, for better readability and adding comments |
281+ | ` re.X ` | Verbose mode, for better readability and adding comments |
282282
283283See [ Python docs - Compilation Flags] ( https://docs.python.org/3/howto/regex.html#compilation-flags ) for more details and long names for flags
284284
@@ -290,21 +290,25 @@ See [Python docs - Compilation Flags](https://docs.python.org/3/howto/regex.html
290290| ` \g<1> ` , ` \g<2> ` , ` \g<3> ` ... | backreferencing matched patterns, prevents ambiguity |
291291| ` \g<0> ` | entire matched portion |
292292
293+ ` \0 ` and ` \100 ` onwards are considered as octal values, hence cannot be used as backreference.
294+
293295<br >
294296
295297### <a name =" pattern-matching-and-extraction " ></a >Pattern matching and extraction
296298
297299To match/extract sequence of characters, use
298300
299301* ` re.search() ` to see if input string contains a pattern or not
300- * ` re.findall() ` to get a list of all matching patterns
302+ * ` re.findall() ` to get a list of all matching portions
303+ * ` re.finditer() ` to get an iterator of ` re.Match ` objects of all matching portions
301304* ` re.split() ` to get a list from splitting input string based on a pattern
302305
303306Their syntax is as follows:
304307
305308``` python
306309re.search(pattern, string, flags = 0 )
307310re.findall(pattern, string, flags = 0 )
311+ re.finditer(pattern, string, flags = 0 )
308312re.split(pattern, string, maxsplit = 0 , flags = 0 )
309313```
310314
@@ -409,6 +413,21 @@ like the matched portion of string, location of matched portion, etc
409413(' bc ac a' , ' c a' )
410414```
411415
416+ * examples for ` re.finditer `
417+
418+ ``` python
419+ >> > m_iter = re.finditer(r ' ( x* ) :( y* ) ' , ' xx:yyy x: x:yy :y' )
420+ >> > [(m[1 ], m[2 ]) for m in m_iter]
421+ [(' xx' , ' yyy' ), (' x' , ' ' ), (' x' , ' yy' ), (' ' , ' y' )]
422+
423+ >> > m_iter = re.finditer(r ' ab+ c' , ' abc ac adc abbbc' )
424+ >> > for m in m_iter:
425+ ... print (m.span())
426+ ...
427+ (0 , 3 )
428+ (11 , 16 )
429+ ```
430+
412431<br >
413432
414433### <a name =" search-and-replace " ></a >Search and Replace
@@ -448,8 +467,8 @@ passed to it, has to be explicity assigned
448467``` python
449468# remove any number of consecutive duplicate words separated by space
450469# quantifiers can be applied to backreferences too!
451- >> > re.sub(r ' \b ( \w + ) ( \1 ) + \b ' , r ' \1 ' , ' a a a walking for for a cause ' )
452- ' a walking for a cause '
470+ >> > re.sub(r ' \b ( \w + ) ( \1 ) + \b ' , r ' \1 ' , ' aa a a a 42 f_1 f_1 f_13.14 ' )
471+ ' aa a 42 f_1 f_13.14 '
453472
454473# add something around the matched strings
455474>> > re.sub(r ' \d + ' , r ' ( \g <0>0) ' , ' 52 apples and 31 mangoes' )
@@ -522,6 +541,7 @@ False
522541* [ CommonRegex] ( https://github.com/madisonmay/CommonRegex ) - collection of common regular expressions
523542* Practice tools
524543 * [ regex101] ( https://regex101.com/ ) - visual aid and online testing tool for regular expressions, select flavor as Python before use
544+ * [ debuggex] ( https://www.debuggex.com ) - railroad diagrams for regular expressions, select flavor as Python before use
525545 * [ regexone] ( https://regexone.com/ ) - interative tutorial
526546 * [cheatsheet](https://www.shortcutfoo.com/app/dojos/python-regex/cheatsheet) - one can also learn it [interactively](https://www.shortcutfoo.com/app/dojos/python-regex)
527547 * [regexcrossword](https://regexcrossword.com/) - practice by solving crosswords, read 'How to play' section before you start
0 commit comments