Need to test if text matches a pattern or extract all matches from a string? This guide shows you how to use the Regex Validator API to validate patterns, extract matches, and get detailed match information including capture groups.
What is Regex Validation?
Regex validation tests whether text matches a specified regular expression pattern. It can answer questions like "Is this a valid email format?" or "Does this string contain a date?" The API can also extract all matches from the text with their positions.
This is useful for input validation, data extraction, and pattern detection.
Match Extraction
Beyond simple yes/no validation, the API returns detailed match information:
- Full match: The complete matched text
- Capture groups: Named and numbered captured portions
- Position: Start and end index of each match
- All matches: Every occurrence in the text (global matching)
(?<name>pattern) syntax for named capture groups. The API returns these with their names.
Using the Regex Validator API
TinyFn provides a comprehensive endpoint for regex validation:
POST https://api.tinyfn.io/v1/regex/validate
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "Contact: john@example.com and jane@test.org",
"pattern": "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
}
{
"is_match": true,
"match_count": 2,
"matches": [
{
"match": "john@example.com",
"index": 9,
"groups": []
},
{
"match": "jane@test.org",
"index": 30,
"groups": []
}
],
"pattern": "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
"flags": "g"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The input text to test (required) |
pattern |
string | Regular expression pattern to match (required) |
flags |
string | Regex flags: g (global), i (case-insensitive), m (multiline). Default: "g" |
full_match |
boolean | Require pattern to match entire text (default: false) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/regex/validate',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Order #12345 and Order #67890',
pattern: 'Order #(\\d+)'
})
}
);
const result = await response.json();
console.log(result.is_match); // true
console.log(result.matches[0].groups); // ["12345"]
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/regex/validate',
json={
'text': 'user@example.com',
'pattern': r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
'full_match': True # Entire string must match
},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['is_match']) # True - it's a valid email format
cURL
curl -X POST "https://api.tinyfn.io/v1/regex/validate" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text":"The year is 2024","pattern":"\\d{4}"}'
Common Use Cases
- Input Validation: Validate emails, phone numbers, URLs
- Data Extraction: Extract dates, numbers, or IDs from text
- Content Analysis: Find patterns in documents
- Log Parsing: Extract structured data from log entries
- Form Validation: Validate user input on forms
Best Practices
- Use full_match for validation: When validating entire inputs like emails
- Handle no-match gracefully: Check is_match before accessing matches array
- Consider case sensitivity: Use 'i' flag when case doesn't matter
- Test your patterns: Regex can behave unexpectedly; test thoroughly
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-regex": {
"url": "https://api.tinyfn.io/mcp/regex/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all regex tools available via MCP in our Regex MCP Tools for AI Agents guide.
Try the Regex Validator API
Get your free API key and start validating patterns instantly.
Get Free API Key