Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Day-02/examples/03-regex-findall.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re

text = "The quick brown fox"
pattern = r"brown"
text = "can you get me some water? fill the water into a bottle"
pattern = r"water"

search = re.search(pattern, text)
if search:
print("Pattern found:", search.group())
# findall() give All matches in a (list)
# group() can't be used with findall() because it gives one result object
findall = re.findall(pattern, text)
if findall:
print("Pattern found:", findall)
else:
print("Pattern not found")
2 changes: 2 additions & 0 deletions Day-02/examples/03-regex-match.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
text = "The quick brown fox"
pattern = r"quick"

# re.match()
# 👉 Checks only at the beginning of the string
match = re.match(pattern, text)
if match:
print("Match found:", match.group())
Expand Down
2 changes: 2 additions & 0 deletions Day-02/examples/03-regex-replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

replacement = "red"

# re.sub() (replace)
# 👉 Replaces matches with something else
new_text = re.sub(pattern, replacement, text)
print("Modified text:", new_text)
2 changes: 2 additions & 0 deletions Day-02/examples/03-regex-search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
text = "The quick brown fox"
pattern = r"brown"

# re.search()
# 👉 Finds the first match anywhere in the string
search = re.search(pattern, text)
if search:
print("Pattern found:", search.group())
Expand Down
2 changes: 2 additions & 0 deletions Day-02/examples/03-regex-split.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
text = "apple,banana,orange,grape"
pattern = r","

# re.split()
# 👉 Splits string based on a pattern
split_result = re.split(pattern, text)
print("Split result:", split_result)