Building Regex Generator using Lyzr SDK
Tags: ai, lyzr, promptengineering, streamlit
Published: 2024-03-27 07:15:00

**Regular expressions** are powerful tools for pattern matching and text processing, but crafting them can be complex and time-consuming. With the advent of automation tools like the **Lyzr SDK** and the flexibility of **Streamlit** for creating interactive applications, generating regular expressions has become more accessible than ever before.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1tvbcl6dajp5pn1cwgo.png)

**What does Regex Generator do** : Our Streamlit application harnesses the Lyzr SDK to provide users with a straightforward interface for generating regular expressions. Users can input their desired patterns or queries, and with the click of a button, the app generates the corresponding regular expression, eliminating the need for manual trial and error.

**Why use Lyzr SDK’s** ?

With **Lyzr SDKs** , crafting your own **GenAI** application is a breeze, requiring only a few lines of code to get up and running swiftly.

[Checkout the Lyzr SDK’s](https://docs.lyzr.ai/homepage)

**Lets get Started!**

Create a new file **app.py** and use that

```
import os
import streamlit as st
from lyzr import FormulaGen
```

This Python script leverages the **os**, **streamlit**, and **lyzr** libraries to build a Streamlit web application. The **os** module provides functionality for interacting with the operating system, while streamlit empowers the creation of interactive data science and machine learning applications.

Next Set Up **OpenAI API Key** and using Streamlit’s secrets management, set up the OpenAI API key within your Streamlit application. Replace "OPENAI_API_KEY" with the actual key name defined in your Streamlit secrets where your OpenAI API key is stored.

```
# Set OpenAI API key
os.environ["OPENAI_API_KEY"] = st.secrets["apikey"]
```

```
# Initialize FormulaGen
generate = FormulaGen()

# Define the Streamlit app
def main():

    # Input field for user to enter the pattern
    pattern_input = st.text_input("Enter your pattern/query :")

    if st.button("Generate Regular Expression"):
        # Generate regular expression using FormulaGen
        result = generate.regular_expression(pattern_input)

        # Display the result
        st.write("Generated Regular Expression:")
        st.write(result)
```
This code initializes an instance of **FormulaGen**, likely a class or function from the **lyzr** library, used for generating regular expressions based on patterns.

Within a **Streamlit** application defined in the main() function, it provides a text input field for users to enter their desired pattern or query.

Upon clicking a button labeled "Generate Regular Expression," the script invokes the regular_expression() method of the **FormulaGen** instance, passing the user input as an argument. The resulting regular expression is then displayed in the **Streamlit** app.

```
# Run the app
if __name__ == "__main__":
    main()
```
This code segment runs the Streamlit web application defined in the main() function when the script is executed directly.

By combining the **Lyzr SDK** with the interactive capabilities of Streamlit, we’ve created a powerful tool that simplifies regular expression generation for users across domains. Whether you’re a data scientist, software engineer, or researcher, our app empowers you to tackle complex text processing tasks with ease.

Ready to streamline your regular expression workflow?

Try out our Streamlit app powered by Lyzr SDK today!

**App link**: [https://regex-generator-lyzr.streamlit.app/](https://regex-generator-lyzr.streamlit.app/)

We welcome your feedback and suggestions for further improvements.

**For more information explore the website**: [Lyzr](https://www.lyzr.ai/)

**Source Code**: [https://github.com/isakshay007/Regex-Generator](https://github.com/isakshay007/Regex-Generator)

**Book your demo**: [https://www.lyzr.ai/book-demo/](https://www.lyzr.ai/book-demo/)

Join the Lyzr community to stay updated on the latest developments and explore additional AI-driven solutions for your data analysis needs.

**Lyzr Community Channels**: [Discord](https://discord.com/invite/nm7zSyEFA2)

**Slack** : [Slack](https://anybodycanai.slack.com/join/shared_invite/zt-2a7fr38f7-_QDOY1W1WSlSiYNAEncLGw#/shared-invite/email)