Using regex in an HTTP Endpoint

I’m trying to extract an email address from the server.request.body_json. This code is in an HTTP Endpoint Server Module. The way I’m trying to do this is by using the following code:

import re

email_regex = re.compile("[\w\.-]+@[\w\.-]+\.\w+")

extracted_email = re.findall(email_regex, str(server.request.body_json)[0])

Now, extracted_email is supposed to have [email@domain.com] as its value, since in the stringified body_json there is an email address. Unfortunately extracted_email remains empty ().

I have tested this code on the Python CLI and when using the following test JSON string:

{‘name’: ‘Some Name’, ‘email’: ‘email@domain.com’, ‘Message’: ‘Some Message’}

and the re.findall code does match an email in the string. I get no errors using this code in Anvil but I seem to be not getting a match with the same data.

I’ve tested the regex with a regex tester to make sure I’m using a good enough regex for an email address (don’t want to complicate myself now using the RFC compliant one). I also validated that the body_json object has the data.

Is there something different in the regex package in Anvil that differs from the Python CLI? I don’t get an error when trying to use this package. Am I using a bad expression to match for an e-mail? Did I miss something?

I have not tried it, but at first glance I would say that this:

re.findall(email_regex, str(server.request.body_json)[0])

should be:

re.findall(email_regex, str(server.request.body_json))[0]
3 Likes

Wow. Well I guess my Python noobishness is showing. Thank you. That’s all it took for it to properly extract the email.