Need to test regular expressions programmatically? This guide covers everything about regex testing via API, including pattern matching, capture groups, flags, and implementation examples in multiple programming languages.
What is a Regular Expression?
A regular expression (regex) is a pattern that describes a set of strings. Regex is used for searching, matching, and manipulating text. It's a powerful tool supported across virtually all programming languages.
Example: \b[A-Z][a-z]+\b matches capitalized words like "Hello", "World"
Regex Features
Key features supported by our regex API:
Capture Groups
Extract specific parts of matches using parentheses. Example: (\d{3})-(\d{4})
Flags
Modifiers like case-insensitive (i), global (g), and multiline (m).
Match Information
Get match positions, full matches, and group captures.
Using the Regex Test API
TinyFn provides a powerful endpoint to test regular expressions:
POST https://api.tinyfn.io/v1/text/regex-test
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"pattern": "\\b(\\w+)@(\\w+\\.\\w+)\\b",
"text": "Contact us at hello@example.com or support@test.org",
"flags": "g"
}
{
"matches": [
{
"match": "hello@example.com",
"index": 14,
"groups": ["hello", "example.com"]
},
{
"match": "support@test.org",
"index": 36,
"groups": ["support", "test.org"]
}
],
"match_count": 2,
"is_valid_pattern": true
}
Parameters
| Parameter | Type | Description |
|---|---|---|
pattern |
string | The regular expression pattern (required) |
text |
string | The text to test against (required) |
flags |
string | Regex flags: g, i, m, s (optional) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/text/regex-test',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
pattern: '\\d{3}-\\d{4}',
text: 'Call 555-1234 or 555-5678',
flags: 'g'
})
}
);
const result = await response.json();
console.log(`Found ${result.match_count} matches`);
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/text/regex-test',
headers={'X-API-Key': 'your-api-key'},
json={
'pattern': r'\d{3}-\d{4}',
'text': 'Call 555-1234 or 555-5678',
'flags': 'g'
}
)
result = response.json()
print(f"Found {result['match_count']} matches")
cURL
curl -X POST "https://api.tinyfn.io/v1/text/regex-test" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"pattern": "\\d{3}-\\d{4}", "text": "Call 555-1234", "flags": "g"}'
Common Use Cases
- Pattern Validation: Test regex patterns before deploying
- Data Extraction: Extract specific data from text
- Input Validation: Validate user input formats
- Text Processing: Find and analyze patterns in text
- Learning Tool: Practice and understand regex patterns
Best Practices
- Escape properly: Double-escape backslashes in JSON strings
- Use raw strings: In Python, use r"pattern" for cleaner patterns
- Test edge cases: Test with various inputs including empty strings
- Avoid catastrophic backtracking: Be careful with nested quantifiers
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-text": {
"url": "https://api.tinyfn.io/mcp/text/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all text analysis tools available via MCP in our Text Analysis MCP Tools for AI Agents guide.
Try the Regex Test API
Get your free API key and start testing regex patterns in seconds.
Get Free API Key