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.
- 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.
- Python 3.8+
pip(Python package installer)
-
Clone the repository:
git clone <YOUR_GITHUB_REPO_URL> cd regex-api
-
Create a virtual environment and install dependencies:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
python main.py
# Or using uvicorn directly:
uvicorn main:app --host 0.0.0.0 --port 8000 --reloadThe API will be accessible at http://0.0.0.0:8000.
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"
]
}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."
}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.
To publish this API on RapidAPI, you will need to:
- Deploy your API: Ensure your API is publicly accessible (e.g., on Railway).
- Create a new API on RapidAPI: Go to the RapidAPI Provider Dashboard and create a new API.
- 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. - Configure Endpoints: RapidAPI will guide you through configuring each endpoint, including setting up parameters and testing.
- 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.
This project is licensed under the MIT License - see the LICENSE file for details. (Note: A LICENSE file will be added later.)