Skip to content

Commit f93a35e

Browse files
authored
Merge branch 'main' into introduction-to-line-charts-in-plotly
2 parents 6d50306 + a8ba894 commit f93a35e

31 files changed

+1227
-96
lines changed
+169-25
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,144 @@
11
## Regular Expressions in Python
2-
Regular expressions (regex) are a powerful tool for pattern matching and text manipulation.
2+
Regular expressions (regex) are a powerful tool for pattern matching and text manipulation.
33
Python's re module provides comprehensive support for regular expressions, enabling efficient text processing and validation.
4+
Regular expressions (regex) are a versitile tool for matching patterns in strings. In Python, the `re` module provides support for working with regular expressions.
45

56
## 1. Introduction to Regular Expressions
6-
A regular expression is a sequence of characters defining a search pattern. Common use cases include validating input, searching within text, and extracting
7+
A regular expression is a sequence of characters defining a search pattern. Common use cases include validating input, searching within text, and extracting
78
specific patterns.
89

910
## 2. Basic Syntax
1011
Literal Characters: Match exact characters (e.g., abc matches "abc").
11-
Metacharacters: Special characters like ., *, ?, +, ^, $, [ ], and | used to build patterns.
12+
Metacharacters: Special characters like ., \*, ?, +, ^, $, [ ], and | used to build patterns.
1213

1314
**Common Metacharacters:**
1415

15-
* .: Any character except newline.
16-
* ^: Start of the string.
17-
* $: End of the string.
18-
* *: 0 or more repetitions.
19-
* +: 1 or more repetitions.
20-
* ?: 0 or 1 repetition.
21-
* []: Any one character inside brackets (e.g., [a-z]).
22-
* |: Either the pattern before or after.
23-
16+
- .: Any character except newline.
17+
- ^: Start of the string.
18+
- $: End of the string.
19+
- *: 0 or more repetitions.
20+
- +: 1 or more repetitions.
21+
- ?: 0 or 1 repetition.
22+
- []: Any one character inside brackets (e.g., [a-z]).
23+
- |: Either the pattern before or after.
24+
- \ : Used to drop the special meaning of character following it
25+
- {} : Indicate the number of occurrences of a preceding regex to match.
26+
- () : Enclose a group of Regex
27+
28+
Examples:
29+
30+
1. `.`
31+
32+
```bash
33+
import re
34+
pattern = r'c.t'
35+
text = 'cat cot cut cit'
36+
matches = re.findall(pattern, text)
37+
print(matches) # Output: ['cat', 'cot', 'cut', 'cit']
38+
```
39+
40+
2. `^`
41+
42+
```bash
43+
pattern = r'^Hello'
44+
text = 'Hello, world!'
45+
match = re.search(pattern, text)
46+
print(match.group() if match else 'No match') # Output: 'Hello'
47+
```
48+
49+
3. `$`
50+
51+
```bash
52+
pattern = r'world!$'
53+
text = 'Hello, world!'
54+
match = re.search(pattern, text)
55+
print(match.group() if match else 'No match') # Output: 'world!'
56+
```
57+
58+
4. `*`
59+
60+
```bash
61+
pattern = r'ab*'
62+
text = 'a ab abb abbb'
63+
matches = re.findall(pattern, text)
64+
print(matches) # Output: ['a', 'ab', 'abb', 'abbb']
65+
```
66+
67+
5. `+`
68+
69+
```bash
70+
pattern = r'ab+'
71+
text = 'a ab abb abbb'
72+
matches = re.findall(pattern, text)
73+
print(matches) # Output: ['ab', 'abb', 'abbb']
74+
```
75+
76+
6. `?`
77+
78+
```bash
79+
pattern = r'ab?'
80+
text = 'a ab abb abbb'
81+
matches = re.findall(pattern, text)
82+
print(matches) # Output: ['a', 'ab', 'ab', 'ab']
83+
```
84+
85+
7. `[]`
86+
87+
```bash
88+
pattern = r'[aeiou]'
89+
text = 'hello world'
90+
matches = re.findall(pattern, text)
91+
print(matches) # Output: ['e', 'o', 'o']
92+
```
93+
94+
8. `|`
95+
96+
```bash
97+
pattern = r'cat|dog'
98+
text = 'I have a cat and a dog.'
99+
matches = re.findall(pattern, text)
100+
print(matches) # Output: ['cat', 'dog']
101+
```
102+
103+
9. `\``
104+
105+
```bash
106+
pattern = r'\$100'
107+
text = 'The price is $100.'
108+
match = re.search(pattern, text)
109+
print(match.group() if match else 'No match') # Output: '$100'
110+
```
111+
112+
10. `{}`
113+
114+
```bash
115+
pattern = r'\d{3}'
116+
text = 'My number is 123456'
117+
matches = re.findall(pattern, text)
118+
print(matches) # Output: ['123', '456']
119+
```
120+
121+
11. `()`
122+
123+
```bash
124+
pattern = r'(cat|dog)'
125+
text = 'I have a cat and a dog.'
126+
matches = re.findall(pattern, text)
127+
print(matches) # Output: ['cat', 'dog']
128+
```
129+
24130
## 3. Using the re Module
25131
26132
**Key functions in the re module:**
27133
28-
* re.match(): Checks for a match at the beginning of the string.
29-
* re.search(): Searches for a match anywhere in the string.
30-
* re.findall(): Returns a list of all matches.
31-
* re.sub(): Replaces matches with a specified string.
134+
- re.match(): Checks for a match at the beginning of the string.
135+
- re.search(): Searches for a match anywhere in the string.
136+
- re.findall(): Returns a list of all matches.
137+
- re.sub(): Replaces matches with a specified string.
138+
- re.split(): Returns a list where the string has been split at each match.
139+
- re.escape(): Escapes special character
140+
Examples:
32141
33-
Examples:
34142
```bash
35143
import re
36144
@@ -45,25 +153,36 @@ print(re.findall(r'\d+', 'abc123def456')) # Output: ['123', '456']
45153
46154
# Substitute matches
47155
print(re.sub(r'\d+', '#', 'abc123def456')) # Output: abc#def#
156+
157+
#Return a list where it get matched
158+
print(re.split("\s", txt)) #['The', 'Donkey', 'in', 'the','Town']
159+
160+
# Escape special character
161+
print(re.escape("We are good to go")) #We\ are\ good\ to\ go
48162
```
49163
50164
## 4. Compiling Regular Expressions
165+
51166
Compiling regular expressions improves performance for repeated use.
52167
53168
Example:
169+
54170
```bash
55171
import re
56172
57173
pattern = re.compile(r'\d+')
58174
print(pattern.match('123abc').group()) # Output: 123
59175
print(pattern.search('abc123').group()) # Output: 123
60176
print(pattern.findall('abc123def456')) # Output: ['123', '456']
177+
61178
```
62179
63180
## 5. Groups and Capturing
181+
64182
Parentheses () group and capture parts of the match.
65183
66184
Example:
185+
67186
```bash
68187
import re
69188
@@ -76,21 +195,46 @@ if match:
76195
```
77196
78197
## 6. Special Sequences
198+
79199
Special sequences are shortcuts for common patterns:
80200
81-
* \d: Any digit.
82-
* \D: Any non-digit.
83-
* \w: Any alphanumeric character.
84-
* \W: Any non-alphanumeric character.
85-
* \s: Any whitespace character.
86-
* \S: Any non-whitespace character.
201+
- \A:Returns a match if the specified characters are at the beginning of the string.
202+
- \b:Returns a match where the specified characters are at the beginning or at the end of a word.
203+
- \B:Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word.
204+
- \d: Any digit.
205+
- \D: Any non-digit.
206+
- \w: Any alphanumeric character.
207+
- \W: Any non-alphanumeric character.
208+
- \s: Any whitespace character.
209+
- \S: Any non-whitespace character.
210+
- \Z:Returns a match if the specified characters are at the end of the string.
211+
87212
Example:
213+
88214
```bash
89215
import re
90216
91217
print(re.search(r'\w+@\w+\.\w+', 'Contact: support@example.com').group()) # Output: support@example.com
92218
```
93219
220+
## 7.Sets
221+
222+
A set is a set of characters inside a pair of square brackets [] with a special meaning:
223+
224+
- [arn] : Returns a match where one of the specified characters (a, r, or n) is present.
225+
- [a-n] : Returns a match for any lower case character, alphabetically between a and n.
226+
- [^arn] : Returns a match for any character EXCEPT a, r, and n.
227+
- [0123] : Returns a match where any of the specified digits (0, 1, 2, or 3) are present.
228+
- [0-9] : Returns a match for any digit between 0 and 9.
229+
- [0-5][0-9] : Returns a match for any two-digit numbers from 00 and 59.
230+
- [a-zA-Z] : Returns a match for any character alphabetically between a and z, lower case OR upper case.
231+
- [+] : In sets, +, \*, ., |, (), $,{} has no special meaning
232+
- [+] means: return a match for any + character in the string.
233+
94234
## Summary
95-
Regular expressions are a versatile tool for text processing in Python. The re module offers powerful functions and metacharacters for pattern matching,
96-
searching, and manipulation, making it an essential skill for handling complex text processing tasks.
235+
236+
Regular expressions (regex) are a powerful tool for text processing in Python, offering a flexible way to match, search, and manipulate text patterns. The re module provides a comprehensive set of functions and metacharacters to tackle complex text processing tasks.
237+
With regex, you can:
238+
1.Match patterns: Use metacharacters like ., \*, ?, and {} to match specific patterns in text.
239+
2.Search text: Employ functions like re.search() and re.match() to find occurrences of patterns in text.
240+
3.Manipulate text: Utilize functions like re.sub() to replace patterns with new text.

0 commit comments

Comments
 (0)