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"
- # Get the first match
- match = re.search(pattern, text)
- # Get all matches
- matches = re.findall(pattern, text)
- # Replace any matches with "replacement"
- text_replaced = re.sub(pattern, "replacement", text)
- # Print the match
- print(match)
- # Print any captured groups
- print(match.groups())
- # Write the replacement text back to the file
- with open('file.txt', 'w') as f:
- f.write(text_replaced)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement