Skip to content

arysweb/regex-gen-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RegexGen API

Overview

RegexGen API is a lightweight tool that generates regular expressions from common natural language patterns and keywords. It's designed for developers who need to quickly retrieve common regex patterns without external dependencies. The API also supports optional test cases to validate the generated patterns.

Features

  • Keyword to Regex: Convert common keywords and phrases into accurate regex patterns.
  • Self-Contained: No external API keys or services required.
  • Test Case Validation: Optionally provide test cases to ensure the generated regex meets your requirements.
  • Clear Explanations: Each generated regex comes with a brief explanation of its functionality.
  • RapidAPI Ready: Designed for easy integration and publishing on platforms like RapidAPI.

Getting Started

Prerequisites

  • Python 3.8+
  • pip (Python package installer)

Installation

  1. Clone the repository:

    git clone <YOUR_GITHUB_REPO_URL>
    cd regex-api
  2. Create a virtual environment and install dependencies:

    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt

Running the API Locally

python main.py
# Or using uvicorn directly:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload

The API will be accessible at http://0.0.0.0:8000.

API Endpoints

GET /

Description: Welcome message for the API, including a list of available patterns.

Response:

{
  "message": "Welcome to RegexGen API. Use /generate to create regex.",
  "available_patterns": [
    "email",
    "url",
    "ipv4",
    "date_iso",
    "phone_us",
    "zip_code",
    "credit_card",
    "hex_color",
    "uuid",
    "password_strong",
    "username",
    "slug"
  ]
}

POST /generate

Description: Generates a regular expression based on a natural language prompt. No external API keys required.

Request Body:

{
  "prompt": "Match a valid email address",
  "test_cases": [
    "test@example.com",
    "invalid-email"
  ]
}

Response (Success):

{
  "regex": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
  "explanation": "Matches standard email addresses.",
  "test_results": [
    {
      "case": "test@example.com",
      "matches": true
    },
    {
      "case": "invalid-email",
      "matches": false
    }
  ]
}

Response (Error):

{
  "detail": "Could not generate regex for the given prompt. Try keywords like 'email', 'url', 'strong password', etc."
}

Deployment to Railway

Railway automatically detects Procfile or uvicorn commands. Ensure your Procfile is set up correctly for production. A simple Procfile could look like this:

web: uvicorn main:app --host 0.0.0.0 --port $PORT

No environment variables are required for this version of the API.

Publishing on RapidAPI

To publish this API on RapidAPI, you will need to:

  1. Deploy your API: Ensure your API is publicly accessible (e.g., on Railway).
  2. Create a new API on RapidAPI: Go to the RapidAPI Provider Dashboard and create a new API.
  3. Import OpenAPI Specification: FastAPI automatically generates an OpenAPI (Swagger) specification at /openapi.json. You can import this directly into RapidAPI to define your endpoints, request bodies, and responses.
  4. Configure Endpoints: RapidAPI will guide you through configuring each endpoint, including setting up parameters and testing.
  5. Monetization and Analytics: Set up your pricing tiers and monitor API usage through RapidAPI's dashboard.

This API includes an X-RapidAPI-User header check (though not strictly enforced in this example) which can be used for RapidAPI specific logic or analytics within your application if needed.

License

This project is licensed under the MIT License - see the LICENSE file for details. (Note: A LICENSE file will be added later.)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors