-1

I am developing an AWS Lambda function where I parse some input with the regex library. Here are the code snippets where it is used:

import regex

def lambda_handler(event, context):
    
    ...

    pattern = r'\{(\w+)\}'
    text_variables = regex.findall(pattern, mail_body)
    for t_var in text_variables:
        if t_var not in variables:
            error_list.append(f"An unregistered variable name was used in the mail body, specifically {t_var}.")

When testing the function, it fails with the error message (the name of the file containing the lambda handler is app.py):

{
  "errorMessage": "Unable to import module 'app': No module named 'regex._regex'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "XXXXX",
  "stackTrace": []
}

First, the runtime settings do say that the handler is app.lambda_handler .

I have tried adding a layer, by downloading the regex library in a folder called 'python', zipping it and uploading it. I have also tried using a folder structure such as python/lib/python3.10/site-packages/{Libraries}. I even tried using specifically the manylinux version, knowing that Lambda runs on Linux.

I have also tried simply downloading the library in the deployment package itself, something that I did in the past and it worked.

Yet, whether I use just one of these "solutions" or both at the same time, the result is the same.

I am using the Python 3.10 runtime.

EDIT: I have played around a bit more and it seems like the Layer does do something, as before adding it it says it cannot find module regex, and after adding it it says it cannot find regex._regex; I know that _regex is a module used in regex, but is it a standalone? What can I do to include it in my dependencies?

2
  • 3
    why not use re.findall() which is part of python standard library ? Commented Sep 7, 2023 at 8:34
  • @rasjani because I was not aware of it. Thank you, now I will go wallow in shame Commented Sep 7, 2023 at 8:47

1 Answer 1

-1

As per the first comment, I simply used re.findall()

It solved my problem, though I would still want to know the root of the problem, as I'm assuming that regex might still be better sometimes (otherwise why would anyone use it?).

Sign up to request clarification or add additional context in comments.

1 Comment

the PyPI "regex" package has "fuzzy" matching. that's the primary reason people used to jump on it. its popularity has waned significantly since then. the built-in re module has always been fine to use for those situations where a regular regular expression did the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.