[[Regular Expressions]] ## Find all matches in a string ```python import re line = '<!-- # https://youtube.com/watch?v=somethinghere -->' link = re.findall('<!-- # (.+) -->', line) print('Source: ',''.join(link)) ``` The `''.join(link)` part is because `re.findall()` returns a list of matches and saves them in the variable `link`. Without `.join()`, the variable is printed as a list, which means it will be in the form: `'[https://youtube.com/watch?v=somethinghere]'` ## Return first match of a regular expression `re.match()` function. Only works for the beginning of the string. ## Find pattern in text `re.search()` . Matches anywhere in the string. ## Troubleshooting ### `AttributeError: 'NoneType' object has no attribute 'group'` This happens when you're returning a group of a match like this: ```python link = re.search(r'!\[\]\((.*)\)', line) print(link.group(1)) ``` When the script encounters a line where no match is returned, it will still try to get the group for the null result, which yields the error. To fix it, do something like this: ```python try: link = re.search(r'!\[\]\((.*)\)', line) print(link.group(1)) except AttributeError: # This is to prevent the AttributeError exception when no matches are returned continue ```