Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. import re
  2. with open('file.txt', 'r') as f:
  3.     text = f.read()
  4. pattern = r"mypattern"
  5. match = re.search(pattern, text)  # Gets the first match
  6. matches = re.findall(pattern, text)  # Gets all matches
  7. text = re.sub(pattern, "replacement", text) # Replaces any matches with "replacement"
  8. print(match) # Prints out the match
  9. print(match.groups()) # Prints out any captured groups
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement