519
519
520
520
For our purpose, we will use the ` search() ` function, and then use ` group() ` to get the output.
521
521
522
+ ` search() ` will scan through a string, looking for any location where this ` re ` matches. It returns __ None__ if no match is found. <br >
523
+ __ Syntax:__ ` search(pattern, string) ` <br >
524
+
525
+ ` group() ` will return the match found in ` search() ` . Defaults to first match<br >
526
+ __ Syntax:__ ` group(index) `
527
+ ` # group() and group(0) will give the same output ` <br >
528
+ In case there are multiple matches found, they can be retrieved using ` group(index) ` where __ index__ starts from ` 0 ` <br >
529
+ To access all the matches as a tuple, use ` groups() ` function.
530
+
531
+ ` . ` is a wild card which will __ match any character except newline__
532
+
522
533
```
523
534
>>> import re
524
535
>>> rhyme=re.search('.ake','I would make a cake, but I hate to bake')
@@ -528,6 +539,14 @@ For our purpose, we will use the `search()` function, and then use `group()` to
528
539
529
540
#### 32. Write a regular expression that will accept an email id. Use the re module.
530
541
542
+ ` . ` is a wild card which will __ match any character except newline__ <br >
543
+ ` [0-9a-zA-Z]+ ` Any character of character class ` 0-9 ` or ` a-z ` or ` A-Z ` any number of times <br >
544
+ ` \ ` is used to escape a special character, in this case ` . ` <br >
545
+ ` () ` is used to specify a group and ` | ` stands for __ or__ <br >
546
+ ` $ ` is end of the string <br >
547
+
548
+ Reference: [ Regular Expressions Cheat Sheet] ( debuggex.com/cheatsheet/regex/python )
549
+
531
550
```
532
551
>>> import re
533
552
>>> e=re.search(r'[0-9a-zA-Z.]+@[a-zA-Z]+\.(com|co\.in)$','abc@gmail.com')
0 commit comments