How do you return a list of the matched item in string with regex? [duplicate]

2024/11/17 18:29:56

I made this simple functions that searches for emails in the source code of a page , the content is just the response taken from get request , now how do you return the matchs in findall as a list with out the \n or ant other unwanted strings

My goal is to get a list of all the matched strings (emails)

def find_emails(content):email_reg =  r"""[a-zA-Z0-9_.]+@+[a-zA-Z0-9]+.+[a-zA-Z0-9]*"""mail_lst = re.findall(email_reg , content)return mail_lst

when the program reaches this for loop i get the emails found in the regex but they are separated by \n and i get some random string in between the emails

I tried using brackets in my regex but this didn't make any difference

if __name__ == "__main__":res = find_emails(content)for item in res:print(item) 
Answer
import re

your regex is wrong , . matches all occurance

def find_emails(content):email_reg =  r"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"mail_lst = re.findall(email_reg , content)return mail_lstprint(find_emails("hiiii@asdasd.££$%%$com"))
print(find_emails("[email protected]"))
https://en.xdnf.cn/q/120171.html

Related Q&A

Indentation Error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

open csv file in python to customize dictionary [duplicate]

This question already has answers here:Creating a dictionary from a CSV file(4 answers)Closed 9 years ago.I would like to know to load this csv file:Epitope,ID,Frequency,AssayAVNIVGYSNAQGVDY,123431,27.…

How does UserPassesTestMixin in django work?

views.pyclass ProfileEdit(UserPassesTestMixin, UpdateView):model = Userform_class = ProfileFormtemplate_name="profile/profile_new.html"def test_func(self):x = self.request.user.idprint (x)y =…

How to extract URL from HTML anchor element using Python3? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Why does using open(filename) fail with filename is not defined?

I want to open a text file containing a column of words and create a list or, alternatively, a string containing these words. Why do I get this error: >>> with open(some_file.txt, r) as some_f…

Read Excel file which has one of the column as Hyperlink through python

I have to read an Excel file Using python. By the time I use xl = pd.ExcelFile("abc.xlsx")The column values which had hyperlink assigned to it becomes a simple number without any hyperlink.Is…

How can I create n number of files in python?

say user gives a number n=3 then I have to create 3 files dynamically. How will I do that? What can be the names of those files. Specifically I want n number of .jpg file created.

For loop doesnt append info correctly into 2D array

I have created an empty 2D array. When I try to add stuff inside of it, it doesnt do so properly. Each index contains the appropriate info, but for some reason, carries the info from the previous into …

How to merge two strings in python?

I need to merge strings together to create one string. For example the strings for "hello" that need to be combined are: [H----], [-E---], [--LL-], and [----O]This is the current code I have …

ValueError: too many values to unpack (expected 3)?

I have been having issues with the code I am trying to right with the model I am trying to code the following error has appeared and being a relative novice I am unsure of how to resolve it.ValueError …