Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- with open('file.txt', 'r') as f:
- text = f.read()
- pattern = r"mypattern"
- match = re.search(pattern, text) # Gets the first match
- matches = re.findall(pattern, text) # Gets all matches
- text = re.sub(pattern, "replacement", text) # Replaces any matches with "replacement"
- print(match) # Prints out the match
- print(match.groups()) # Prints out any captured groups
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement